使用循环按要求打印值
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),然后得到打印的表。
但是我被卡住了如何实现 如果我的逻辑是正确的,如上所述,则在代码中进行此操作。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用2个嵌套循环进行操作:
或一个单个循环:
输出:
You can do it with 2 nested loops:
Or one single loop:
Output:
这是一个简单的示例,显示JavaScript中的逻辑。
它不使用双循环或智能递增的条件语句中的智能增量,但它仅使用带有三元运算符的条件(在每次迭代时翻转的标志上)将索引从1增加到10,以输出一种时尚或另一个。
这不是最优雅的解决方案,它使用乘法。我敢肯定,您会发现最聪明的方法来实现同样的方法。
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.
这是另一个变化:
它没有做任何太聪明的事情:仍然使用%交替,只是先预先分配重复行。这将有两个连续的循环,而不是嵌套。
Here's yet another variation:
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.