嘿,伙计们,只是看看过去的一些任务,我不知道为什么我的时间循环不工作。我需要输入一个数字,并在1到10之间输入一个数字。
int n;
Scanner sc = new Scanner(System.in);
do {
System.out.print("Enter number 1&10");
n = sc.nextInt();
}
while (n>=1 && n<=10);
System.out.print("Validated number = "+ n);发布于 2019-09-19 16:25:39
您的条件是“在值在我们想要的范围内时继续迭代”。当然,您希望在值超出所需范围时继续向用户请求更多的输入:
do {
System.out.print("Enter number 1&10");
n = sc.nextInt();
} while (n < 1 || n > 10);始终考虑条件要表示什么,并记住,如果条件为真,则循环将继续进行。
https://stackoverflow.com/questions/58015271
复制相似问题