Java 不匹配异常故障

Java 不匹配异常故障,java,exception,try-catch,java.util.scanner,mismatch,Java,Exception,Try Catch,Java.util.scanner,Mismatch,不知道如何实现不匹配异常。当一个字母应该是int并且没有程序崩溃时,我如何键入它 try { System.out.print("Enter graduation year : "); year = response.nextInt(); } catch (InputMismatchException ex) { while (year < 1920 || year > 2010) { System.out.print("invalid value. Enter a

不知道如何实现不匹配异常。当一个字母应该是int并且没有程序崩溃时,我如何键入它

try { 
  System.out.print("Enter graduation year : ");
  year = response.nextInt();
} catch (InputMismatchException ex) {
  while (year < 1920 || year > 2010) {
  System.out.print("invalid value. Enter a year between 1920 and 2010 : " );
  year = response.nextInt();
}
试试{
系统输出打印(“输入毕业年份:”;
年份=响应。nextInt();
}捕获(输入不匹配异常){
而(年份<1920年|年>2010年){
System.out.print(“无效值。输入1920到2010年之间的年份:”);
年份=响应。nextInt();
}

不,您不想在catch块中获取输入,而是想警告用户该块中的错误输入。相反,请在扫描仪的
nextInt()
后的try块中设置一个布尔值。将整个内容环绕在while循环中,只有在成功设置布尔值后才退出while循环

伪代码:

initialize boolean variable, inputStillBad to true. 
while inputStillBad
  try block
    prompt user for input
    get input from scanner
    if this line was reached, then scanner succeeded
    check if year is within bounds, and if so:
      set inputStillBad to false.
    else:
      warn user that the year was out of range
  catch your exception here
    warn the user that they entered non-numeric input
end while loop

不,您不想在catch块中获取输入,而是想警告用户该块中的错误输入。相反,请在扫描仪的
nextInt()
之后的try块中设置一个布尔值。在while循环中环绕整个内容,只有在成功设置布尔值后才退出while循环

伪代码:

initialize boolean variable, inputStillBad to true. 
while inputStillBad
  try block
    prompt user for input
    get input from scanner
    if this line was reached, then scanner succeeded
    check if year is within bounds, and if so:
      set inputStillBad to false.
    else:
      warn user that the year was out of range
  catch your exception here
    warn the user that they entered non-numeric input
end while loop

不,您不想在catch块中获取输入,而是想警告用户该块中的错误输入。相反,请在扫描仪的
nextInt()
之后的try块中设置一个布尔值。在while循环中环绕整个内容,只有在成功设置布尔值后才退出while循环

伪代码:

initialize boolean variable, inputStillBad to true. 
while inputStillBad
  try block
    prompt user for input
    get input from scanner
    if this line was reached, then scanner succeeded
    check if year is within bounds, and if so:
      set inputStillBad to false.
    else:
      warn user that the year was out of range
  catch your exception here
    warn the user that they entered non-numeric input
end while loop

不,您不想在catch块中获取输入,而是想警告用户该块中的错误输入。相反,请在扫描仪的
nextInt()
之后的try块中设置一个布尔值。在while循环中环绕整个内容,只有在成功设置布尔值后才退出while循环

伪代码:

initialize boolean variable, inputStillBad to true. 
while inputStillBad
  try block
    prompt user for input
    get input from scanner
    if this line was reached, then scanner succeeded
    check if year is within bounds, and if so:
      set inputStillBad to false.
    else:
      warn user that the year was out of range
  catch your exception here
    warn the user that they entered non-numeric input
end while loop

您可以检查用户是否输入了Int,如下所示:

if(response.hasNextInt())
{
 year = response.nextInt();
}

您可以检查用户是否输入了Int,如下所示:

if(response.hasNextInt())
{
 year = response.nextInt();
}

您可以检查用户是否输入了Int,如下所示:

if(response.hasNextInt())
{
 year = response.nextInt();
}

您可以检查用户是否输入了Int,如下所示:

if(response.hasNextInt())
{
 year = response.nextInt();
}