String[][] board = [a,b,c,d]
[e,f,g,h];
for(int i=0; i<board.length; i++){
String temp = "";
for(int j=0; j<board[i].length; j++){
temp = temp+board[i][j];
System.out.println(temp);
}
}
电流输出
a
ab
abc
abcd
e
ef
efg
efgh
我希望输出看起来像
a
ab
abc
abcd
b
bc
bcd
c
cd
d
e
ef
efg
efgh
f
fg
fgh
g
gh
h
我该怎么做?
最佳答案
您需要第三个嵌套 for 循环来执行此操作:
String[][] board = [a,b,c,d]
[e,f,g,h];
// i - for each row
for(int i=0; i<board.length; i++){
// j - start from this column in a row
for(int j=0; j<board[i].length; j++){
String temp = "";
// put all columns right to the j and including together
for(int k=j;k<board[i].length; k++) {
temp = temp+board[i][k];
System.out.println(temp);
}
}
}
关于java - 如何操作嵌套的 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20268556/