我可以使用NumberNumberFormateXeption。即使用户输入字符串或非数字,该程序也应继续
猜数字
import java.util.*;
public class LabExer5A
{
public static void main(String args[])
{
Scanner Input = new Scanner (System.in);
System.out.println("Guess a number between 1 and 50");
int JPS_Guess = Input.nextInt();
int JPS_CorrectNum = 25;
int JPS_NextGuess;
do
{
try
{
if (JPS_Guess == JPS_CorrectNum)
{
System.out.println("You got it in ");
}
else if (JPS_Guess < JPS_CorrectNum && JPS_Guess >= 1)
{
System.out.println("Too low. Try again.");
JPS_NextGuess = Input.nextInt();
JPS_Guess = JPS_NextGuess;
}
else if (JPS_Guess > JPS_CorrectNum && JPS_Guess <= 50)
{
System.out.println("Too High. Try again.");
JPS_NextGuess = Input.nextInt();
JPS_Guess = JPS_NextGuess;
}
else if(JPS_Guess < 1 || JPS_Guess > 50)
{
throw new InputMismatchException();
}
}
catch(InputMismatchException e)
{
System.out.println("Invalid value. Please enter a number between 1 and 50: ");
JPS_NextGuess = Input.nextInt();
JPS_Guess = JPS_NextGuess;
}
catch (NumberFormatException ex)
{
System.out.println("Error - Enter Numerical Values Only");
JPS_NextGuess = Input.nextInt();
JPS_Guess = JPS_NextGuess;
}
}
while (JPS_Guess != JPS_CorrectNum);//will rerun the program until the user guess the correct number
}
}
}
}
我不知道我在哪里犯了错误。要么是使用了 throw catch,要么是因为我重新运行程序的方式。我对使用异常和 try-catch 还很陌生,请帮助并友善。谢谢。
Guessing the number
import java.util.*;
public class LabExer5A
{
public static void main(String args[])
{
Scanner Input = new Scanner (System.in);
System.out.println("Guess a number between 1 and 50");
int JPS_Guess = Input.nextInt();
int JPS_CorrectNum = 25;
int JPS_NextGuess;
do
{
try
{
if (JPS_Guess == JPS_CorrectNum)
{
System.out.println("You got it in ");
}
else if (JPS_Guess < JPS_CorrectNum && JPS_Guess >= 1)
{
System.out.println("Too low. Try again.");
JPS_NextGuess = Input.nextInt();
JPS_Guess = JPS_NextGuess;
}
else if (JPS_Guess > JPS_CorrectNum && JPS_Guess <= 50)
{
System.out.println("Too High. Try again.");
JPS_NextGuess = Input.nextInt();
JPS_Guess = JPS_NextGuess;
}
else if(JPS_Guess < 1 || JPS_Guess > 50)
{
throw new InputMismatchException();
}
}
catch(InputMismatchException e)
{
System.out.println("Invalid value. Please enter a number between 1 and 50: ");
JPS_NextGuess = Input.nextInt();
JPS_Guess = JPS_NextGuess;
}
catch (NumberFormatException ex)
{
System.out.println("Error - Enter Numerical Values Only");
JPS_NextGuess = Input.nextInt();
JPS_Guess = JPS_NextGuess;
}
}
while (JPS_Guess != JPS_CorrectNum);//will rerun the program until the user guess the correct number
}
}
}
}
I'm not sure where I made the mistake. Either the use of the throw catch or maybe because of the way I rerun the program. I'm pretty new to using exception and try-catch Please help and be kind. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
nextInt()
实际上输入其他数据类型时抛出InputMismatchException在非整数输入中,另一件事是
input.nextint()
应始终在try/catch中。在您的代码中,第一个输入和输入任何错误的值之后,将不会捕获例外。我修改了您的代码更简单。我建议您使用布尔值来跟踪是否猜到了这个数字,因此您不必互换猜测。此外,良好的凹痕格式有助于阅读。
nextInt()
actually throws InputMismatchException when you input other data type so you can directly use the catch for InputMismatchException and you should addInput.next()
so that the scanner will take in the non integer inputAnother thing is that
Input.nextInt()
should always be inside the try/catch. In your code, the exception will not be catched for the first input and after you input any wrong value.I have modified your code to be simpler. I suggest you use boolean to keep track of whether the number is guessed so you don't have to interchange the guess. Also, good indentation formatting helps reading.