有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java提示用户输入正确的输入类型

while (goodInput=false)
        {
            try
            {
                System.out.println("How long is the word you would like to guess?");
                wordSize=scan.nextInt();
                while(wordSize>word.longestWord())
                {
                    System.out.println("There are no words that big! Please enter another number");
                    wordSize=scan.nextInt();
                }
                goodInput=true;
            }
            catch(InputMismatchException ime)
            {
                System.out.println("Thats not a number! Try again");
            }

        }

我试图提示用户输入一个数字,但我无法让它正常运行。我希望它一直运行,直到输入正确的输入


共 (4) 个答案

  1. # 1 楼答案

    首先,

    while (goodInput=false) 
    

    在将false赋值给goodInput时,必须使用==运算符检查goodInput是否为false

    while (goodInput==false)
    

    或者只是

    while (!goodInput) would suffice
    

    以下是java中对Equality Operator的引用

  2. # 2 楼答案

    while循环中的条件需要

     while(goodinput == false)
    

    您所做的是将false分配给goodinput,最终结果为false。请参见以下语句的输出

    boolean a;
    System.out.println((a = false));
    

    你需要一个equality operator在那里

  3. # 3 楼答案

    你必须写作

    while (goodInput == false)
    

    甚至更好

    while (!goodInput)
    

    而不是

    while (goodInput = false)
    

    第一个将goodInput的值与false进行比较,第二个将goodInput的值取反,您的版本将false分配给goodInput

  4. # 4 楼答案

    一个问题是:

    while (goodInput=false)
    

    false分配给goodInput,使之成为while(false),从而导致循环根本不执行

    换成

    while (!goodInput)