英文:
Trouble setting a Parameter
问题
我对如何只允许输入1至4的数字感到困惑。我不确定是否有一个术语来描述这个,我认为这是一个参数。
我要询问的代码是倒数第三行
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);
}
英文:
Im confused as to how I allow only numbers 1-4? Im not sure if there is a term for this, i think its parameter
THE CODE IM QUESTIONING IS THE 3RD TO LAST LINE
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);
}
答案1
得分: 1
Use while (number > 4 || number < 1);
This disallows anything outside of the range.
英文:
Use while (number > 4 || number < 1);
This disallows anything outside of the range.
答案2
得分: 0
Scanner scanner = new Scanner(System.in);
int number;
do {
System.out.print("请输入一个正数:");
while (!scanner.hasNextInt()) {
String input = scanner.next();
System.out.printf("\"%s\" 不是一个有效的数字。\n", input);
}
number = scanner.nextInt();
} while (number > 4 || number < 1);
System.out.printf("您输入了一个正数 %d。\n", number);
英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论