【发布时间】:2019-07-27 13:41:20
【问题描述】:
我正在尝试通过扫描仪和行打印包含列的表格。当然,for 循环应该从 0 开始,但我希望它从 1 开始计数以进行打印。请帮我正确打印代码。我得到空值和数字金字塔。
Output needed when n = 4 inputted:
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
import java.util.Scanner;
public class Testing {
public static void main(String[] args) {
System.out.print("Type any variable?");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
String[] arr = new String[n + 1];
String s = "";
for (int count = 1; count <= 10; count++) {
for (int col = 1; col <= n; col++) {
s = count + "\t";
arr[col] += s;
System.out.println(arr[col]);
}
}
}
}
【问题讨论】:
-
我不明白你的硬编码
<= 10...为什么是10?另外,您知道System.out.println()和System.out.print()之间的区别吗?如果没有,那么这是您需要为此任务学习的一件事,或者至少非常方便。 -
我以10为例。
-
那么你没有清楚地描述输出的要求。例如,对于输入值“20”,您会期望什么?
-
再次,我以10为例。
-
什么例子?