我试图提示用户输入 1 到 12 之间的值。如果他们输入超出此范围的值 (EX -15),我需要继续提示,直到他们输入 1-12 之间的值。此外,当用户输入指定范围 (1-12) 之间的值时,它需要打印一个时间表,其中包含整数 1 乘以输入数字的结果。到目前为止,我相信我的布局是正确的,但我知道我遗漏了一些东西,但无法弄清楚这里的代码是什么:
package times_Table;
import java.util.Scanner;
public class Times_Table {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
final int TOTAL = 5;
System.out.print("Enter an integer between 1 and 12:");
int input = sc.nextInt();
int numbers = input;
//If the input is out of the range 1-12 keep going
//If the input is in the range of 1-12 stop
//Need to find a way to keep looping if the integer inputed is out of the range 1-12
while (numbers>1 && numbers<12) {
if (numbers<1 && numbers>12) {
for (int i = 1; i <= TOTAL; i++) {
for (int j = 1; j<=TOTAL; j++) {
System.out.print(i*j + "\t");
}
System.out.println();
}
} else {
if (numbers < 1 && numbers > 12) {
System.out.print("Enter an integer between 1 and 12:");
}
}
}
}
}
最佳答案
像这样更改 while:
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();
}
}
关于java - 尝试让用户输入介于两个值之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35734177/