【问题标题】:Multiple conditions while loop for scanner validation用于扫描仪验证的多个条件 while 循环
【发布时间】:2020-07-06 05:46:57
【问题描述】:

大家好,尝试验证 Java 中用户输入的整数范围。我是编程和java的新手。我遇到困难的部分是倍数和接近尾声。我不确定如何处理它,所以已经猜到了。我无法在其他地方找到合适的解决方案;

    import java.util.Scanner;


  class Main {
  public static void main(String[] args) {
   
   // Create a Scanner 
   Scanner sc = new Scanner(System.in);
    int number;
     do {
    System.out.println("Please enter window width");
    while (!sc.hasNextInt()) {
        System.out.println("That's not a number!");
        sc.next(); // this is important!
    }
    number = sc.nextInt();
     } while ((width > 0.5 & <2.5) && (height >0.5 & <3.5));  
   System.out.println("Window is:" + height + "m high " + width + "m wide.");
    }

【问题讨论】:

  • while((width &gt; 0.5 &amp;&amp; width &lt; 2.5) &amp;&amp; (height &gt; 0.5 &amp;&amp; height &lt; 3.5))
  • 澄清:&amp; 进行按位与比较,例如。 G。 0b1001 &amp; 0b1010 == 0b1000.&amp;&amp; 是布尔 AND,如果两个操作数都为真,则返回真。

标签: java loops while-loop


【解决方案1】:

您的 do while 循环语法似乎不正确,请尝试以下代码:

do {
System.out.println("Please enter window width");
while (!sc.hasNextInt()) {
    System.out.println("That's not a number!");
    sc.next(); // this is important!
}
number = sc.nextInt();
 } 
while((width > 0.5 && width < 2.5) && (height > 0.5 && height < 3.5));

【讨论】:

  • widthheight 在循环中保持不变。
【解决方案2】:

使用Double#parseDouble 解析输入,如果不成功(即如果抛出异常),则回送以要求用户重试。验证输入(是否为数字)后,根据您的要求验证范围。

按如下方式进行:

import java.util.Scanner;

class Main {
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {

        // Variables for width and height
        double width, height;

        // Input width
        do {
            width = getDimennsion("Please enter window width between 0.5 & 2.5: ");
        } while (width < 0.5 || width > 2.5);// Loop back in case of invalid dimension

        // Input height
        do {
            height = getDimennsion("Please enter window height between 0.5 & 3.5: ");
        } while (height < 0.5 || height > 3.5);// Loop back in case of invalid dimension

        System.out.println("Width: " + width + ", Height: " + height);
        // ...Rest of the processing
    }

    static double getDimennsion(String msg) {
        boolean valid;
        double num = 0;
        do {
            valid = true;
            System.out.print(msg);
            try {
                // Get input
                num = Double.parseDouble(sc.nextLine());
            } catch (IllegalArgumentException e) {
                System.out.println("Invalid input. Please try again");
                valid = false;
            }
        } while (!valid);// Loop back in case of invalid input
        return num;
    }
}

示例运行:

Please enter window width between 0.5 & 2.5: a
Invalid input. Please try again
Please enter window width between 0.5 & 2.5: 10
Please enter window width between 0.5 & 2.5: 2
Please enter window height between 0.5 & 3.5: b
Invalid input. Please try again
Please enter window height between 0.5 & 3.5: 15
Please enter window height between 0.5 & 3.5: 3
Width: 2.0, Height: 3.0

【讨论】:

  • 只是一个建议,您可以发布相关代码而不是发布整个演示类。对于演示,您可以提供ideone.com。这将使您的答案简短明了。
  • @Code_Mode - 我不喜欢使用第三方在线工具发布代码,因为该在线工具不受 SO 的控制,可能会被关闭。
猜你喜欢
  • 2013-11-25
  • 1970-01-01
  • 2019-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多