Java 验证输入并防止try/catch退出

Java 验证输入并防止try/catch退出,java,validation,exception,input,while-loop,Java,Validation,Exception,Input,While Loop,我是java新手,但我用这个特殊的程序做了很多实验,我遇到了麻烦。此程序的目的是捕获用户输入的值是否超过变量限制,并验证输入是否满足我指定的范围 如果输入超出变量限制,而没有while循环,则程序退出 如果输入超过了while循环的变量限制,它将无限循环 我尝试过使用while循环,我的三个结果如下: 用户输入程序员指定的有效数字,一切正常 用户输入一个大于100的数字,并提示重试 用户超过了变量限制,程序进入一个 无限循环 如果我把一个逃生装置放在抓块里,它的作用就和while不在那里一样

我是java新手,但我用这个特殊的程序做了很多实验,我遇到了麻烦。此程序的目的是捕获用户输入的值是否超过变量限制,并验证输入是否满足我指定的范围

  • 如果输入超出变量限制,而没有while循环,则程序退出
  • 如果输入超过了while循环的变量限制,它将无限循环
我尝试过使用while循环,我的三个结果如下:

  • 用户输入程序员指定的有效数字,一切正常
  • 用户输入一个大于100的数字,并提示重试
  • 用户超过了变量限制,程序进入一个 无限循环
  • 如果我把一个逃生装置放在抓块里,它的作用就和while不在那里一样。我希望程序打印错误,但允许用户重试一个值

    while(!success){
        try{
            sh = sc.nextShort();
            while(sh < 1 || sh > 100){
                System.out.print("You must enter a value between 1-100: ");
                sh = sc.nextShort();
            } //close try
            System.out.println("Great job!");
            success = true; //escape the while
        }catch{Exception ex){
            System.out.println("ERROR: " + ex);
            success = false; //causes infinite loop... I understand
        } //close catch
    } //close while
    
    while(!success){
    试一试{
    sh=sc.nextShort();
    而(sh<1 | | sh>100){
    System.out.print(“您必须输入一个介于1-100之间的值:”);
    sh=sc.nextShort();
    }//结束尝试
    System.out.println(“干得好!”);
    success=true;//退出while
    }catch{Exception ex){
    System.out.println(“错误:+ex”);
    success=false;//导致无限循环…我理解
    }//紧扣
    }//正在关闭
    
    如果我正确理解了你的问题,你可以去掉第二个while循环并用If替换它。然后你可以打印出值必须为1-100,并抛出和异常,以使你的程序执行catch语句并打印出你抛出的错误

    while(!success){
        try{
            sh = sc.nextShort();
            if(sh < 1 || sh > 100){
                System.out.print("You must enter a value between 1-100");
                throw Exception;
            } //close try
            System.out.println("Great job!");
            success = true; //escape the while
        }catch{Exception ex){
            System.out.println("ERROR: " + ex);
            success = false; //causes infinite loop... I understand
        } //close catch
    } //close while
    
    while(!success){
    试一试{
    sh=sc.nextShort();
    如果(sh<1 | | sh>100){
    System.out.print(“您必须输入一个介于1-100之间的值”);
    抛出异常;
    }//结束尝试
    System.out.println(“干得好!”);
    success=true;//退出while
    }catch{Exception ex){
    System.out.println(“错误:+ex”);
    success=false;//导致无限循环…我理解
    }//紧扣
    }//正在关闭
    
    如果我正确理解了你的问题,你可以去掉第二个while循环并用If替换它。然后你可以打印出值必须为1-100,并抛出和异常,以使你的程序执行catch语句并打印出你抛出的错误

    while(!success){
        try{
            sh = sc.nextShort();
            if(sh < 1 || sh > 100){
                System.out.print("You must enter a value between 1-100");
                throw Exception;
            } //close try
            System.out.println("Great job!");
            success = true; //escape the while
        }catch{Exception ex){
            System.out.println("ERROR: " + ex);
            success = false; //causes infinite loop... I understand
        } //close catch
    } //close while
    
    while(!success){
    试一试{
    sh=sc.nextShort();
    如果(sh<1 | | sh>100){
    System.out.print(“您必须输入一个介于1-100之间的值”);
    抛出异常;
    }//结束尝试
    System.out.println(“干得好!”);
    success=true;//退出while
    }catch{Exception ex){
    System.out.println(“错误:+ex”);
    success=false;//导致无限循环…我理解
    }//紧扣
    }//正在关闭
    
    我理解您的问题,在传递较大的值(如之类的较大值,例如“1000000000”)时,您将面临一个异常循环

    在无限循环中有如下异常:

    ERROR: java.util.InputMismatchException: For input string: "10000000000"
    ERROR: java.util.InputMismatchException: For input string: "10000000000"
    
    但您希望程序打印一个异常行,然后允许用户再次输入

    您可以通过下面的一种方法从您使用的扫描仪中读取错误输入(String bad_input=sc.next();)

     while (!success) {
                      try {
                            sh = sc.nextShort();
                            while (sh < 1 || sh > 100) {
                                  System.out.print("You must enter a value between 1-100: ");
                                  sh = sc.nextShort();
                            } // close try
                            System.out.println("Great job!");
                            success = true; // escape the while
                      } catch (Exception ex) {
                            System.out.println("ERROR: " + ex);
                            success = false; // causes infinite loop... I understand
                            String bad_input = sc.next();// your bad input to allow read that exception
                      } // close catch
                } // close while
    
    while(!success){
    试一试{
    sh=sc.nextShort();
    而(sh<1 | | sh>100){
    System.out.print(“您必须输入一个介于1-100之间的值:”);
    sh=sc.nextShort();
    }//结束尝试
    System.out.println(“干得好!”);
    success=true;//退出while
    }捕获(例外情况除外){
    System.out.println(“错误:+ex”);
    success=false;//导致无限循环…我理解
    String bad_input=sc.next();//允许读取该异常的错误输入
    }//紧扣
    }//正在关闭
    
    可以找到此问题的原因:

    如果翻译成功,扫描仪将通过输入 匹配


    我理解您的问题,在传递更大的值(如“1000000000”)时,您将面临一个异常循环

    在无限循环中有如下异常:

    ERROR: java.util.InputMismatchException: For input string: "10000000000"
    ERROR: java.util.InputMismatchException: For input string: "10000000000"
    
    但您希望程序打印一个异常行,然后允许用户再次输入

    您可以通过下面的一种方法从您使用的扫描仪中读取错误输入(String bad_input=sc.next();)

     while (!success) {
                      try {
                            sh = sc.nextShort();
                            while (sh < 1 || sh > 100) {
                                  System.out.print("You must enter a value between 1-100: ");
                                  sh = sc.nextShort();
                            } // close try
                            System.out.println("Great job!");
                            success = true; // escape the while
                      } catch (Exception ex) {
                            System.out.println("ERROR: " + ex);
                            success = false; // causes infinite loop... I understand
                            String bad_input = sc.next();// your bad input to allow read that exception
                      } // close catch
                } // close while
    
    while(!success){
    试一试{
    sh=sc.nextShort();
    而(sh<1 | | sh>100){
    System.out.print(“您必须输入一个介于1-100之间的值:”);
    sh=sc.nextShort();
    }//结束尝试
    System.out.println(“干得好!”);
    success=true;//退出while
    }捕获(例外情况除外){
    System.out.println(“错误:+ex”);
    success=false;//导致无限循环…我理解
    String bad_input=sc.next();//允许读取该异常的错误输入
    }//紧扣
    }//正在关闭
    
    可以找到此问题的原因:

    如果翻译成功,扫描仪将通过输入 匹配


    问题是,扫描仪存储整个输入字符串并对其进行处理。 当您的输入不是短值而是字符串时,它会引发异常