【问题标题】:how to prompting the user to enter a correct value after he already entered a wrong(out of range) value?在用户输入错误(超出范围)值后如何提示用户输入正确值?
【发布时间】:2015-06-24 07:39:42
【问题描述】:
System.out.println("Enter the first test score:");

double test1 = input.nextInt();

if (!(test1 >= 0  && test1 <= 100))
System.out.println("This is out of the acceptable range, please enter a  number between 0 and 100 .");

System.out.println("Enter the second test score:");

double test2 = input.nextInt();

if (!(test2 >= 0  && test2 <= 100))
System.out.println("This is out of the acceptable range, please enter a number between 0 and 100 .");

如果我提示用户输入 test1 的值及其在特定范围内的 nor,我如何再次提示他输入正确的值?现在如果我运行并输入一个错误的值“这超出了可接受的范围,请输入一个介于 0 和 100 之间的数字”消息会出现,但它会直接进入 test2。我试着这样做

System.out.println("Enter the first test score:");

double test1 = input.nextInt();

if (!(test1 >= 0  && test1 <= 100))
System.out.println("This is out of the acceptable range, please enter a  number between 0 and 100 .");
double test1 = input.nextInt();

但我收到一条错误消息。 我是否必须使用循环,如果是这样怎么办?

【问题讨论】:

  • 使用 while 循环,例如 while(!(test2 &gt;= 0 &amp;&amp; test2 &lt;= 100)),不断请求新的输入。
  • but i get an error msg. 那条错误消息说什么?
  • 循环并在不匹配时使用 continue 并在匹配时中断并到达 test2 条件。

标签: java range prompt


【解决方案1】:

那是因为你声明了两次test1

double test1 = input.nextInt();

if (!(test1 >= 0  && test1 <= 100))
System.out.println("This is out of the acceptable range, please enter a  number between 0 and 100 .");
-->  double test1 = input.nextInt(); //Look at this line

只需尝试以下操作: 双 test1 = input.nextInt();

if (!(test1 >= 0  && test1 <= 100))
System.out.println("This is out of the acceptable range, please enter a  number between 0 and 100 .");
test1 = input.nextInt(); 

对于更简洁的代码,请使用以下方法:

public double getValue(){
        double test1=input.nextInt();
        if (!(test1 >= 0  && test1 <= 100)){
            return getValue();
        }else{
            return test1;
        }
    }

除非根据!(test1 &gt;= 0 &amp;&amp; test1 &lt;= 100) 条件输入正确,否则上述代码将继续接受用户输入。

【讨论】:

    【解决方案2】:

    首先,您要声明 test1 两次,其次 - 您可以使用循环

        double test1 = input.nextInt();
        while(!(test1 >= 0 && test1 <= 100))
        {
            System.out.println("This is out of the acceptable range, please enter a  number between 0 and 100 .");
            /*double */test1 = input.nextInt();
        }
    

    【讨论】:

      【解决方案3】:

      试试类似的东西

      double test1 = input.nextInt();
      while(!(test1 >= 0 && test1 <= 100)) {
           System.out.println("Enter a valid number"); 
           test1.nextInt();
      }
      

      这个想法是提示用户直到他输入一个有效的输入。如果您使用简单的 if 语句或固定的 for 循环,您最终只会要求固定数量的正确值。

      您应该记住的另一件事是:

      if (!(test1 >= 0  && test1 <= 100))
      System.out.println("This is out of the acceptable range, please enter a  number between 0 and 100 .");
      double test1 = input.nextInt(); //There is no need to declare test1 again. Remove double
      

      将被编译为

      if (!(test1 >= 0  && test1 <= 100))
           System.out.println("This is out of the acceptable range, please enter a  number between 0 and 100 .");
      
      test1 = input.nextInt(); //This is not part of the if!!!!
      

      所以你必须使用括号,如

      if (!(test1 >= 0  && test1 <= 100)) {
          System.out.println("This is out of the acceptable range, please enter a  number between 0 and 100 .");
          test1 = input.nextInt(); 
      }
      

      【讨论】:

        猜你喜欢
        • 2019-06-17
        • 1970-01-01
        • 1970-01-01
        • 2021-03-05
        • 1970-01-01
        • 2023-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多