【问题标题】:Input validation (do-while loop) malfunctioning [closed]输入验证(do-while循环)出现故障[关闭]
【发布时间】:2014-03-17 15:08:55
【问题描述】:
===== CS302 TOOL BOX =====
T > COIN TOSS SIMULATOR
G > GRADE ESTIMATOR
C > COLOR CHALLENGE
Q > QUIT
Type code letter for your choices: h
Invalid selection. Please try agian: 
t
Invalid selection. Please try agian: 
T

^这是我运行程序并给出错误猜测时得到的结果。循环是为了验证用户输入,所以它完全失败了。关于我做错了什么的任何想法?感谢您的宝贵时间!

}

    System.out.println("===== CS302 TOOL BOX =====");
    System.out.println("T > COIN TOSS SIMULATOR");
    System.out.println("G > GRADE ESTIMATOR");
    System.out.println("C > COLOR CHALLENGE");
    System.out.println("Q > QUIT");
    System.out.print("Type code letter for your choices: ");
    boolean code_letter;
    String code_choice = scanner.next();
    do {
        if (code_choice.toUpperCase().equals("Q")
                || (code_choice.toUpperCase()).equals("C")
                || (code_choice.toUpperCase()).equals("G")
                || (code_choice.toUpperCase()).equals("T")) {
            code_letter = true;
        }

        else {
            System.out.println("Invalid selection. Please try agian: ");
            code_letter = false;
            scanner.next();
        }
    } while (!(code_letter));

    {

        System.out.println("you did it?");
    }

}
}

【问题讨论】:

  • 请不要在得到答案后用垃圾文字代替您的问题。将问题留给未来的读者学习。
  • 如果某个答案对您有所帮助,请单击旁边的复选标记接受该答案是有礼貌的。 stackoverflow.com/help/accepted-answer

标签: java validation input while-loop


【解决方案1】:

do-while 循环中初始化code_choice 变量,否则在条件测试之前循环的每次迭代都不会重新初始化该变量

String code_choice = scanner.next();

喜欢这个

//declared it outside to take care of the scope
String code_choice = null;
    do {
        //initialize it every time before you perform the conditional test.
        //This increases the readability!
        code_choice = scanner.next();
        if (code_choice.toUpperCase().equals("Q")
                || (code_choice.toUpperCase()).equals("C")
                || (code_choice.toUpperCase()).equals("G")
                || (code_choice.toUpperCase()).equals("T")) {
            code_letter = true;
        }

        else {
            System.out.println("Invalid selection. Please try agian: ");
            code_letter = false;
        }
    } while (!(code_letter));

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    我假设扫描仪(小写 S)是一个 Scanner 对象?下次请把所有相关代码都包含进去,否则问题很难调试。

    您遇到的主要问题是 else 条件中的行。 scanner.next() 不做任何事情。你需要这样做:

    else {
        System.out.println("Invalid selection. Please try agian: ");
        code_letter = false;
        code_choice = scanner.next(); // This needs to be modified
    }
    

    这应该可以正常工作。

    【讨论】:

      【解决方案3】:

      试试吧..

      把你的

      scanner.next()
      

      在 if 之前,删除包围

      的花括号
      System.out.println("you did it?"); 
      

      最后你的while...把它改成..

      while(code_letter !=  true);
      

      【讨论】:

        猜你喜欢
        • 2021-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多