我试图提示用户输入1到12之间的值。如果他们输入的值超出此范围(EX -15),我需要继续提示,直到他们输入的值为1-12。此外,当用户输入指定范围(1-12)之间的值时,需要打印一个时间表,其中包含整数1乘以输入数字的结果。到目前为止,我相信我的布局有些正确,但我知道我错过了一些东西,但无法弄清楚这里的代码是什么:
{{1}}
答案 0 :(得分:1)
像这样改变时间:
while (keepGoing) {
while (numbers < 1 || numbers > 12) {
System.out.print("Enter an integer between 1 and 12:");
numbers = sc.nextInt();
}
for (int i = 1; i <= numbers; i++) {
for (int j = 1; j<=numbers; j++) {
System.out.print(i*j + "\t");
}
System.out.println();
}
}
答案 1 :(得分:1)
重复提示的推荐模式是do-while循环。
Scanner sc = new Scanner(System.in);
int input;
do {
System.out.print("Enter an integer between 1 and 12: ");
input = sc.nextInt();
if (input < 1 || input >= 12) {
System.out.println("Invalid number input!");
}
} while (input < 1 || input >= 12);
这会打印一张表
for (int i = 1; i <= input; i++) {
for (int j = 1; j <= input; j++) {
System.out.printf("%3d\t", i*j);
}
System.out.println();
}
喜欢这样
Enter an integer between 1 and 12: 11
1 2 3 4 5 6 7 8 9 10 11
2 4 6 8 10 12 14 16 18 20 22
3 6 9 12 15 18 21 24 27 30 33
4 8 12 16 20 24 28 32 36 40 44
5 10 15 20 25 30 35 40 45 50 55
6 12 18 24 30 36 42 48 54 60 66
7 14 21 28 35 42 49 56 63 70 77
8 16 24 32 40 48 56 64 72 80 88
9 18 27 36 45 54 63 72 81 90 99
10 20 30 40 50 60 70 80 90 100 110
11 22 33 44 55 66 77 88 99 110 121