设置参数时出现问题



我很困惑如何只允许数字1-4?我不确定是否有这个术语,我认为它的参数

代码IM问题是倒数第三行

private void validatePositiveNumber() {
Scanner scanner = new Scanner(System.in);
int number;
do {
System.out.print("Please enter a positive number: ");
while (!scanner.hasNextInt()) {
String input = scanner.next();
System.out.printf(""%s" is not a valid number.n", input);
}
number = scanner.nextInt();
} while (number < 4);
System.out.printf("You have entered a positive number %d.n", number);
}

使用while (number > 4 || number < 1);这不允许任何超出范围的内容。

Scanner scanner = new Scanner(System.in);
int number;
do {
System.out.print("Please enter a positive number: ");
while (!scanner.hasNextInt()) {
String input = scanner.next();
System.out.printf(""%s" is not a valid number.n", input);
}
number = scanner.nextInt();
} while (number > 4 || number < 1);
System.out.printf("You have entered a positive number %d.n", number);

相关内容

  • 没有找到相关文章

最新更新