当用户给出最后一个数字并将变量添加在一起以获得新变量时,如何打印出数字表?例如,如果用户为 numberOfRows 指定值 3,则示例输出将为:
1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
这是我到目前为止所拥有的:
int numberOfRows;
int numberOfVariables = numberOfRows*numberOfRows;
int i = 0;
boolean cycle = false;
if(i < numberOfVariables){
cycle = true;
i++;
} else if(i >= numberOfVariables){
cycle = false;
i++;
}
System.out.println(cycle);
for(cycle = true;;){
System.out.println(i + "\t");
i++;
}
我不知道如何继续。
最佳答案
public static void main(String args[]) throws NumberFormatException,
IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number of rows : ");
int n = Integer.parseInt(reader.readLine());
for (int i = 0; i < n; i++) {
for (int j = i; j < n + i; j++) {
System.out.print(j + "\t");
}
System.out.println();
}
}
输出:
输入行数:6
0 1 2 3 4 5
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
5 6 7 8 9 10
关于java - 打印一个包含 n*n 个数字的表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27118211/