验证输入和防止尝试/赶上退出

问题描述:

我是新来的java,但我已经试验了很多这个特定的程序,我打了一堵墙。该程序的目的是捕捉用户是否输入超出变量限制的值,并验证输入是否满足我指定的范围。验证输入和防止尝试/赶上退出

  • 如果输入超过变量限制而没有while循环,程序将退出。
  • 如果输入超出了while循环的变量限制,它将无限循环。

我用while循环和我的三个结果尝试此:

  1. 用户输入一个程序员指定有效数字,一切都很好。
  2. 用户输入一个高于100的数字,并提示再次尝试...
  3. 用户超出变量限制,程序进入 无限循环。

如果我在catch块中放置一个转义,它的作用就好像不存在时一样。我希望程序打印错误,但允许用户重试一个值。

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 
+0

我不知道我理解你的问题,但我怀疑你的问题是,第一'SH = sc.nextShort();'在try-block中。如果那是你所有的代码,我作为一个用户不会知道我必须输入一些东西,所以我会等待程序打印一些东西,程序会等待我的输入 - 一个典型的死锁。我怀疑发生错误时会发生同样的情况,即它会打印错误,然后再次启动外部循环的主体,该主体开始等待输入数字。打印出错后,一个简单的“修复”就是打印“再试一次:”这样的内容。 – Thomas

+0

[使用java.util.Scanner验证输入]的可能重复(https://*.com/questions/3059333/validating-input-using-java-util-scanner) – Tom

+0

问题是,当引发异常时,它会无限循环错误消息。我通过在catch中创建一个新的“Try again:”提示符和扫描器来跟踪下面的帖子。它很实用,但我仍然有很多学习要做,以便准确理解发生了什么。 感谢您的建议! – kaygeee

的问题是输入 ,该扫描仪存储整个输入字符串并对其进行处理。 当你的输入不是一个短值,而是一个字符串时,它会引发异常。您在catch中记录异常,并在下一个循环中尝试从同一个存储的字符串中读取一个短值,然后再次抛出异常.... - >无限循环。

如果创建在catch一个新的Scanner,它会再次从控制台读取:

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); 
      sc = new Scanner(System.in); 
     } //close catch 
    } 
+0

我测试了这个,它工作!我仍然不是很了解,但我已经标记为正确,我会做更多的学习。谢谢! – kaygeee

如果我正确理解你的问题,你可以摆脱你的第二个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 

据我了解你的问题,你面对异常的环路上传递一个更大的值(更大的东西像e.g“百亿”)。

你有例外未来如下面的死循环:

ERROR: java.util.InputMismatchException: For input string: "10000000000" 
ERROR: java.util.InputMismatchException: For input string: "10000000000" 

但是你想程序打印一个例外线,然后再允许用户输入。

您可以做到这一点下面的读取错误的输入(字符串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 

这个问题的原因可以发现here

如果转换成功,则扫描器执行匹配