Java 为什么我的try-catch方法没有显示我为输入的字符串而不是整数编写的错误消息?(爪哇) System.out.println(“”); System.out.println(“第4阶段:输入一个介于5和500之间的整数:”); IntEntry=keyedInput.nextInt(); 如果(强度>5&&IntEntry5&&IntEntry5&&intEntry
上面的代码是我试图得到一个用户输入的介于5和500之间的有效整数。如果用户输入的是字符串而不是整数,则程序应使用try-catch方法为用户输出消息。try-catch方法不适用于我,它输出了一条错误消息。有人能告诉我为什么try-catch方法在我的代码中不起作用吗?它抛出一个Java 为什么我的try-catch方法没有显示我为输入的字符串而不是整数编写的错误消息?(爪哇) System.out.println(“”); System.out.println(“第4阶段:输入一个介于5和500之间的整数:”); IntEntry=keyedInput.nextInt(); 如果(强度>5&&IntEntry5&&IntEntry5&&intEntry,java,loops,while-loop,try-catch,Java,Loops,While Loop,Try Catch,上面的代码是我试图得到一个用户输入的介于5和500之间的有效整数。如果用户输入的是字符串而不是整数,则程序应使用try-catch方法为用户输出消息。try-catch方法不适用于我,它输出了一条错误消息。有人能告诉我为什么try-catch方法在我的代码中不起作用吗?它抛出一个输入不匹配异常的状态。不是NumberFormatException。(你把它和Integer.parseInt混为一谈了)。像这样更新你的代码 System.out.println(" "); Sy
输入不匹配异常的状态。不是NumberFormatException
。(你把它和Integer.parseInt混为一谈了)。像这样更新你的代码
System.out.println(" ");
System.out.println("Stage 4: Enter an integer between 5 and 500:");
IntEntry = keyedInput.nextInt();
if(IntEntry > 5 && IntEntry < 500)
{
System.out.println("You will go to stage 5.");
}
else
{
try
{
while(IntEntry < 5 || IntEntry > 500)
{
System.out.println("Try Again. Enter an integer that is between 5 and 500 characters long. “z”.");
IntEntry = keyedInput.nextInt();
}
}
catch(NumberFormatException e)
{
System.out.println("Try Again. Enter an integer between 5 and 500:.");
}
}
System.out.println(“”);
System.out.println(“第4阶段:输入一个介于5和500之间的整数:”);
试试{
IntEntry=keyedInput.nextInt();
如果(强度>5&&IntEntry<500)
{
System.out.println(“您将进入第5阶段”);
}
其他的
{
尝试
{
而(强度<5 | |强度>500)
{
System.out.println(“重试。输入一个长度在5到15个字符之间的字符串,并且不包含字母“z”);
IntEntry=keyedInput.nextInt();
}
}捕获(数字格式)
{
System.out.println(“重试。输入一个长度在5到15个字符之间的字符串,并且不包含字母“z”);
}
}
}捕获(数字格式)
{
System.out.println(“重试。输入一个长度在5到15个字符之间的字符串,并且不包含字母“z”);
}
使用时,您应该捕获输入不匹配异常,而不是数字格式异常。此外,第一次使用nextInt()
不在try-catch块内。此外,当您稍后捕获异常时,代码不再循环。您可以使用do while循环和标志(例如,valid
)干净地执行此操作。只有当整数可以解析且在范围内时,才会设置该标志。因此,循环将继续,直到用户输入范围内的有效整数。下面是一个完整的示例:
System.out.println(" ");
System.out.println("Stage 4: Enter an integer between 5 and 500:");
try{
IntEntry = keyedInput.nextInt();
if(IntEntry > 5 && IntEntry < 500)
{
System.out.println("You will go to stage 5.");
}
else
{
try
{
while(IntEntry < 5 || IntEntry > 500)
{
System.out.println("Try Again. Enter a string that is between 5 and 15 characters long, and does not contain the letter “z”.");
IntEntry = keyedInput.nextInt();
}
}catch(NumberFormatException e)
{
System.out.println("Try Again. Enter a string that is between 5 and 15 characters long, and does not contain the letter “z”.");
}
}
}catch(NumberFormatException e)
{
System.out.println("Try Again. Enter a string that is between 5 and 15 characters long, and does not contain the letter “z”.");
}
Scanner Scanner=新的扫描仪(System.in);
System.out.println(“第4阶段:输入一个介于5和500之间的整数:”);
内部意图;
布尔有效=假;
做{
试一试{
intEntry=scanner.nextInt();
如果(强度>5&&intEntry<500){
valid=true;//设置标志以离开循环
}否则{
System.out.println(“整数超出范围。请重试。输入一个介于5和500之间的整数:”;
}
}捕获(输入不匹配异常e){
System.out.println(“无法分析整数。请重试。输入一个介于5和500之间的整数:”);
scanner.next();//清除无效令牌
}
}而(!有效);
System.out.println(“您将进入第5阶段”);
你认为是什么代码抛出了NFE?@Newusersda我刚刚更新了答案。此代码经过测试,应按预期工作。
Scanner scanner = new Scanner(System.in);
System.out.println("Stage 4: Enter an integer between 5 and 500:");
int intEntry;
boolean valid = false;
do {
try {
intEntry = scanner.nextInt();
if (intEntry > 5 && intEntry < 500) {
valid = true; // set flag to leave loop
} else {
System.out.println("Integer out of range. Try Again. Enter an integer between 5 and 500:");
}
} catch (InputMismatchException e) {
System.out.println("Could not parse integer. Try again. Enter an integer between 5 and 500:");
scanner.next(); // clear invalid token
}
} while (!valid);
System.out.println("You will go to stage 5.");