如何一直捕获异常,直到它';正确的java

如何一直捕获异常,直到它';正确的java,java,exception,try-catch,java.util.scanner,inputmismatchexception,Java,Exception,Try Catch,Java.util.scanner,Inputmismatchexception,我正在编写一个使用Scanner类的程序,并希望使用try-catch块捕获输入不匹配异常。这是我写的: public class StudentInfo{ public static void main(String[] args){ Scanner scnr = new Scanner(System.in); int creditHours = 0; try { System.out.println("Please enter the nu

我正在编写一个使用Scanner类的程序,并希望使用try-catch块捕获输入不匹配异常。这是我写的:

public class StudentInfo{

 public static void main(String[] args){
    Scanner scnr = new Scanner(System.in);
    int creditHours = 0;
    try
     {
       System.out.println("Please enter the number of credit hours that you currently have (do not count credit hours from classes that you are currently attending this semester)");
       creditHours = scnr.nextInt();
     }
    catch(InputMismatchException e){
       System.out.println("CLASSIFICATION ERROR: NUMBER NOT RECOGNIZED. ENTER AN INTEGER FOR CREDIT HOURS");
       Scanner input = new Scanner(System.in);
       creditHours = input.nextInt();
    }
    String studentClass = checkStudent (creditHours);
    System.out.println("Official Student Classification: " + studentClass);
    }
try-catch块只工作一次,如中所示,如果我第一次输入24.5,它会捕获异常并让用户重新键入他们的信用小时数,但如果他们第二次重新键入非整数,它将无法再次捕获错误并发送相应的消息。因此,本质上,我想知道是否有任何方法可以让它捕获异常并发送错误消息,无论他们尝试了多少次。我尝试过使用do-while循环或while语句,但它不起作用。另外,我在catch块中创建了一个新的scanner变量,因为如果不是,它不允许我在出于某种原因发出错误消息后输入一个新的整数。它实际上会抛出我键入的错误,然后继续给我Java的InputMismatchException错误

以下是我使用while循环的尝试:

int creditHours = 0;
    while(creditHours <= 0){
    try
     {
       System.out.println("Please enter the number of credit hours that you currently have (do not count credit hours from classes that you are currently attending this semester)");
       creditHours = scnr.nextInt();
     }
    catch(InputMismatchException e){
       System.out.println("CLASSIFICATION ERROR: NUMBER NOT RECOGNIZED. ENTER AN INTEGER FOR CREDIT HOURS");
       Scanner input = new Scanner(System.in);
       creditHours = input.nextInt();
    }
   }

    String studentClass = checkStudent (creditHours);
    System.out.println("Official Student Classification: " + studentClass);
  }
int creditHours=0;

while(creditHours您需要try-catch处于循环中,以便重新运行代码

试试这个:

 public class StudentInfo{

 public static void main(String[] args){

    int creditHours = 0;
    boolean ok = false;
    System.out.println("Please enter the number of credit hours that you currently have (do not count credit hours from classes that you are currently attending this semester)");

    while (!ok)
    {
        try
         {
           Scanner scnr = new Scanner(System.in);
           creditHours = scnr.nextInt();
           ok = true;
         }
        catch(InputMismatchException e){
           System.out.println("CLASSIFICATION ERROR: NUMBER NOT RECOGNIZED. ENTER AN INTEGER FOR CREDIT HOURS");
        }
    }


    String studentClass = checkStudent (creditHours);
    System.out.println("Official Student Classification: " + studentClass);
    }

try/catch不是循环。如果想要循环行为,必须添加循环。例如,
while(1){try…catch…}
@MarcB正如我在问题中所说,我已经做了一段时间和一段时间的循环,但两个循环都不起作用。它的作用完全相同。那么,展示你的循环尝试。@MarcB它已经在那里了,在他的问题的底部。非常感谢!!!它起作用了,我完全理解它。非常感谢!