使用循环按要求打印值

发布于 2025-01-24 03:19:01 字数 632 浏览 2 评论 0原文

hi问题,我如何在控制台上从处理中打印出来。

    int result;
    for (int i=100 ; i < = 10; i + = 10) { 
       result = 100; 
       for (int k=2; k <= 10; k++) { 
          result-=10 ; 
       } print(result);
        println();
    }
  • 我的想法是它看起来像10*10桌子,因此I和K是&lt; = 10。
  • 另一个想法是当我奇怪的时候,我从100和 - = 10开始(i&gt; = 10) 当我什至(第2,4,6号线...)时,我从10开始, += 10(i&lt; = 100),然后得到打印的表。

但是我被卡住了如何实现 如果我的逻辑是正确的,如上所述,则在代码中进行此操作。

print out below table in console using a nested loop

Hi question how do I print out above in console from processing.

    int result;
    for (int i=100 ; i < = 10; i + = 10) { 
       result = 100; 
       for (int k=2; k <= 10; k++) { 
          result-=10 ; 
       } print(result);
        println();
    }
  • My thought is it looks like a 10*10 table thus both i and k is < =
    10.
  • And other thought is when i is odd i starts from 100 and -=10 (i > = 10)
    when i is even(line 2,4,6...) i start from 10 and +=10 ( i<=100) Then I got the printed table.

But I am stuck how to achieve
this in code if my logic is correct as mentioned above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伴我老 2025-01-31 03:19:01

您可以使用2个嵌套循环进行操作:

for (int i=0; i<10; ++i) { 
     for (int j=0; j<10; ++j) {
        print(str(i%2 == 0 ? (10-j)*10 : (j+1)*10) + " ");
     }
     println();
}

或一个单个循环:

for (int i=0; i<100; ++i) { 
     int j = i%10;
     print(str((i/10)%2 == 0 ? (10-j)*10 : (j+1)*10) + " ");
     if (i % 10 == 9) {
       println();
     }
}

输出:

  100 90 80 70 60 50 40 30 20 10 10 
10 20 30 40 50 60 70 80 90 100 
100 90 80 70 60 50 40 30 20 10 10 
10 20 30 40 50 60 70 80 90 100 
100 90 80 70 60 50 40 30 20 10 10 
10 20 30 40 50 60 70 80 90 100 
100 90 80 70 60 50 40 30 20 10 10 
10 20 30 40 50 60 70 80 90 100 
100 90 80 70 60 50 40 30 20 10 10 
10 20 30 40 50 60 70 80 90 100
 

You can do it with 2 nested loops:

for (int i=0; i<10; ++i) { 
     for (int j=0; j<10; ++j) {
        print(str(i%2 == 0 ? (10-j)*10 : (j+1)*10) + " ");
     }
     println();
}

Or one single loop:

for (int i=0; i<100; ++i) { 
     int j = i%10;
     print(str((i/10)%2 == 0 ? (10-j)*10 : (j+1)*10) + " ");
     if (i % 10 == 9) {
       println();
     }
}

Output:

100 90 80 70 60 50 40 30 20 10 
10 20 30 40 50 60 70 80 90 100 
100 90 80 70 60 50 40 30 20 10 
10 20 30 40 50 60 70 80 90 100 
100 90 80 70 60 50 40 30 20 10 
10 20 30 40 50 60 70 80 90 100 
100 90 80 70 60 50 40 30 20 10 
10 20 30 40 50 60 70 80 90 100 
100 90 80 70 60 50 40 30 20 10 
10 20 30 40 50 60 70 80 90 100
靖瑶 2025-01-31 03:19:01

这是一个简单的示例,显示JavaScript中的逻辑。

它不使用双循环或智能递增的条件语句中的智能增量,但它仅使用带有三元运算符的条件(在每次迭代时翻转的标志上)将索引从1增加到10,以输出一种时尚或另一个。

这不是最优雅的解决方案,它使用乘法。我敢肯定,您会发现最聪明的方法来实现同样的方法。

let alt = true;
let counter = 0;

//infinite loop
while(true){
  counter++;
  let row = "";
  for(let i=1; i<=10; i++){
    row += (alt) ? (10*i) + ' ' : 110-(10*i) + ' ';
  }
  //output current row
  console.log(row);
  //switch alternate
  alt = !alt;
  //exit at iteration 10
  if(counter == 10) break;
}

This is a simple example showing that logic in javascript.

It doesn't use the double loop nor smart increments inside the for condition statement, but it just increments an index from 1 to 10 using a condition with the ternary operator (over a flag that gets flipped at each iteration) to output one fashion or the other.

It's not the most elegant solution and it uses multiplications. I'm sure you'll find smartest ways to achieve the same.

let alt = true;
let counter = 0;

//infinite loop
while(true){
  counter++;
  let row = "";
  for(let i=1; i<=10; i++){
    row += (alt) ? (10*i) + ' ' : 110-(10*i) + ' ';
  }
  //output current row
  console.log(row);
  //switch alternate
  alt = !alt;
  //exit at iteration 10
  if(counter == 10) break;
}

治碍 2025-01-31 03:19:01

这是另一个变化:

void setup() {
  print(reversingNumberLoop(10, 100, 10, 9));
}

String reversingNumberLoop(int from, int to, int increment, int rows){
  
  String result = "";
  
  String backToFront = "";
  String frontToBack = "";
  
  // loop once writing in both directions
  for(int i = from; i <= to; i+= increment){
    frontToBack += i + " ";
    backToFront += ((to - i) + from) + " ";
  }
  
  // add line endings
  frontToBack += "\n";
  backToFront += "\n";
  // repeat for the number of rows
  for(int i = 0 ; i < rows; i++){
    result += i % 2 == 1 ? frontToBack : backToFront;
  }
  
  return result;
}

它没有做任何太聪明的事情:仍然使用%交替,只是先预先分配重复行。这将有两个连续的循环,而不是嵌套。

Here's yet another variation:

void setup() {
  print(reversingNumberLoop(10, 100, 10, 9));
}

String reversingNumberLoop(int from, int to, int increment, int rows){
  
  String result = "";
  
  String backToFront = "";
  String frontToBack = "";
  
  // loop once writing in both directions
  for(int i = from; i <= to; i+= increment){
    frontToBack += i + " ";
    backToFront += ((to - i) + from) + " ";
  }
  
  // add line endings
  frontToBack += "\n";
  backToFront += "\n";
  // repeat for the number of rows
  for(int i = 0 ; i < rows; i++){
    result += i % 2 == 1 ? frontToBack : backToFront;
  }
  
  return result;
}

It's not doing anything too clever: still using % to alternate and just pre-allocates the repeating lines first. This would have two loops in succession, not nested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文