gpt4 book ai didi

java - 输入必须输入两次,并且可以输入字符串时不允许输入

转载 作者:行者123 更新时间:2023-11-30 01:59:47 25 4
gpt4 key购买 nike

我的代码

package morrisJCh5Sec2;

import java.util.Scanner;

public class Ch5Sec2 {
public static int collectInteger(String purpose, int minimum, int maximum) {

Scanner input = new Scanner(System.in);
System.out.println(purpose);
System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");
int value = input.nextInt();
while(input.hasNext()) {

if(!input.hasNextInt()) {
System.out.println("The value you enter needs to be between " + minimum + " and " + maximum + ". Please try again.");
System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");
value = input.nextInt();
input.next();
continue;
//not an integer
}//end not int if
else {
value = input.nextInt();
if(value >= minimum && value <= maximum) {
return value;
}
else {
System.out.println("The value you enter needs to be between " + minimum + " and " + maximum + ". Please try again.");
System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");
value = input.nextInt();
}//end else out of bounds

}
//input.close();
}

return 0;
}//end collectInteger
public static void main(String args[]) {
final int LOW_INT = 0;
final int HIGH_INT = 100;
int inputValue = collectInteger("Enter the number of cats.", LOW_INT, HIGH_INT);
System.out.printf("Your number of cats was %d.\n", inputValue);
}//end main
}//end class

我的输出:

Enter the number of cats.
Enter an integer between 0 and 100: -56
-56
The value you enter needs to be between 0 and 100. Please try again.
Enter an integer between 0 and 100: 101
101
The value you enter needs to be between 0 and 100. Please try again.
Enter an integer between 0 and 100: jads
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at morrisJCh5Sec2.Ch5Sec2.collectInteger(Ch5Sec2.java:30)
at morrisJCh5Sec2.Ch5Sec2.main(Ch5Sec2.java:42)

我的输出应该允许输入一个字符串,并且它会再次要求输入一个整数。另一个问题是我的代码不应该让用户输入两次输入,它应该只接受用户输入一次并运行代码。

最佳答案

您的代码中有很多对 next()nextInt() 的额外调用。您可以将代码转换为 do-while 循环。此外,您还可以在第一个 if 中额外调用 next()。然后在内部 else 中,您要求用户输入 nextInt(),但由于您在循环开始时调用 nextInt(),这是一个额外的,这就是为什么它要求输入两次。此外,在第一个 if 中,它会处理输入是否不是可解析的 int。如果不是,则有 value = input.nextInt(),这将导致输入不匹配异常。相反,调用空白 next() 来清除错误的输入:

public static int collectInteger(String purpose, int minimum, int maximum) {

Scanner input = new Scanner(System.in);
System.out.println(purpose);
System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");
int value;
do {
if(!input.hasNextInt()) {
System.out.println("The value you enter needs to be between " + minimum + " and " + maximum + ". Please try again.");
System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");
input.next(); //clear bad input
}
else {
value = input.nextInt();
if(value >= minimum && value <= maximum) {
return value;
}
else {
System.out.println("The value you enter needs to be between " + minimum + " and " + maximum + ". Please try again.");
System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");

}

}

} while(input.hasNext());
return 0;
}

示例输出:

Enter the number of cats.
Enter an integer between 0 and 100: -51
The value you enter needs to be between 0 and 100. Please try again.
Enter an integer between 0 and 100: 20000
The value you enter needs to be between 0 and 100. Please try again.
Enter an integer between 0 and 100: Hello
The value you enter needs to be between 0 and 100. Please try again.
Enter an integer between 0 and 100: 4
Your number of cats was 4.

关于java - 输入必须输入两次,并且可以输入字符串时不允许输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53269192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com