[英]How do I make this table 0-9 with loops in Java?
How do I make this table? 我如何制作这张桌子?
This is what should be the outcome: 结果应该是这样的:
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 0
2 3 4 5 6 7 8 9 0 1
3 4 5 6 7 8 9 0 1 2
4 5 6 7 8 9 0 1 2 3
5 6 7 8 9 0 1 2 3 4
6 7 8 9 0 1 2 3 4 5
7 8 9 0 1 2 3 4 5 6
8 9 0 1 2 3 4 5 6 7
9 0 1 2 3 4 5 6 7 8
And this is the best I could come up with: 这是我能想到的最好的方法:
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++) {
if (i + j < 10) {
System.out.print(i + j);
} else
System.out.print("x");
}
System.out.println();
}
}
}
I simply can't find a solution how to get numbers running again past 9 with the beginning of 0,1,2,3 etc. My code would generate next: 我根本找不到解决方案,如何让数字以9、0、1、2、3等开头再次超过9。我的代码将生成下一个:
0123456789
123456789x
23456789xx
3456789xxx
456789xxxx
56789xxxxx
6789xxxxxx
789xxxxxxx
89xxxxxxxx
9xxxxxxxxx
You can do: 你可以做:
System.out.print((i + j) % 10);
To turn 10
into 0
, 11
into 1
, etc. 将
10
变成0
,将11
变成1
,依此类推。
Try this: 尝试这个:
for (int i=0; i<10; i++) {
for (int j=i; j<i+10; j++) {
System.out.print(j%10);
}
System.out.println();
}
The key here is to use %
(modulo) operator. 此处的关键是使用
%
(模)运算符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.