So I'm kind of new to Java and decided to create a sliding number puzzle of some sort. Here's what I have :
所以我对Java很新,并决定创建某种滑动数字拼图。这就是我所拥有的:
int[] puz = {1,2,3,
4,5,6,
7,8,9}
for(int i=0; i<puz.length; i++){
System.out.println(puz[i]);
}
The 1 is supposed to be the blank spot but I'll figure that out later. My problem is that the code prints:
1应该是空白点,但我稍后会想到这一点。我的问题是代码打印:
1
2
3
4
5
6
7
8
9
when I want it to print:
当我想要它打印时:
1 2 3
4 5 6
7 8 9
I've also tried doing a nested loop that I'm too embarrassed to show on here due to how hideous it was. Would I try using a 2d array instead?
我也试过做一个嵌套的循环,由于它有多可怕,我太尴尬了。我会尝试使用2D阵列吗?
3 个解决方案
#1
1
I guess you could try...
我想你可以试试......
int puz = {1,2,3,4,5,6,7,8,9};
int n = Math.ceil(Math.sqrt(puz.length));
for (int i = 0; i < puz.length; i++) {
System.out.print(puz[i] + ((i + 1) % n == 0 ? "\r\n" : " ");
}
#2
1
Try creating a variable counter and increment it every time you iterate through the loop. Using a modulus operator, divide it by 3 and when remainder is 0, create a new line.
尝试创建一个变量计数器,并在每次遍历循环时递增它。使用模数运算符,将其除以3,当余数为0时,创建一个新行。
int puz = {1,2,3,4,5,6,7,8,9};
int counter = 1;
for(int i=0; i<puz.length; i++){
System.out.print(puz[i]);
if (counter % 3 == 0){
System.out.println("");
}
counter++;
}
#3
0
The trick here is to use the modulus operator. This operator divides one number by another, and returns the remainder. In java (and everywhere else as far as I know), %
is the modulus operator. If you want every third number to have a line break after it, simply divide by three using modulus division, like so:
这里的技巧是使用模数运算符。此运算符将一个数字除以另一个数字,并返回余数。在java(以及据我所知的其他地方)中,%是模数运算符。如果你希望每个第三个数字后面都有换行符,只需用模数除法除以3,如下所示:
int[] puz = {1,2,3,4,5,6,7,8,9};
//For what it's worth, you don't have this semicolon in your question, so I added it in.
for(int i=0; i<puz.length; i++){
System.out.print(puz[i] + " ");
if(i % 3 == 2){//It's equal to 2 because you start at 0 and not 1.
System.out.println("");
}
}
This code, when executed, prints the following, which is what you wanted:
此代码在执行时会打印以下内容,这是您想要的:
1 2 3
4 5 6
7 8 9
#1
1
I guess you could try...
我想你可以试试......
int puz = {1,2,3,4,5,6,7,8,9};
int n = Math.ceil(Math.sqrt(puz.length));
for (int i = 0; i < puz.length; i++) {
System.out.print(puz[i] + ((i + 1) % n == 0 ? "\r\n" : " ");
}
#2
1
Try creating a variable counter and increment it every time you iterate through the loop. Using a modulus operator, divide it by 3 and when remainder is 0, create a new line.
尝试创建一个变量计数器,并在每次遍历循环时递增它。使用模数运算符,将其除以3,当余数为0时,创建一个新行。
int puz = {1,2,3,4,5,6,7,8,9};
int counter = 1;
for(int i=0; i<puz.length; i++){
System.out.print(puz[i]);
if (counter % 3 == 0){
System.out.println("");
}
counter++;
}
#3
0
The trick here is to use the modulus operator. This operator divides one number by another, and returns the remainder. In java (and everywhere else as far as I know), %
is the modulus operator. If you want every third number to have a line break after it, simply divide by three using modulus division, like so:
这里的技巧是使用模数运算符。此运算符将一个数字除以另一个数字,并返回余数。在java(以及据我所知的其他地方)中,%是模数运算符。如果你希望每个第三个数字后面都有换行符,只需用模数除法除以3,如下所示:
int[] puz = {1,2,3,4,5,6,7,8,9};
//For what it's worth, you don't have this semicolon in your question, so I added it in.
for(int i=0; i<puz.length; i++){
System.out.print(puz[i] + " ");
if(i % 3 == 2){//It's equal to 2 because you start at 0 and not 1.
System.out.println("");
}
}
This code, when executed, prints the following, which is what you wanted:
此代码在执行时会打印以下内容,这是您想要的:
1 2 3
4 5 6
7 8 9