繁体   English   中英

为什么我的 java 程序跳过我的输入并挂在 IF 语句上?

[英]Why does my java program skip my input and hang on the IF statements?

除了这个单一方法,我的程序中的其他所有东西都可以工作,它跳过 sc.next() 然后挂在我的 if 上。

public static String getChoice() {
    boolean choiceSelector = false;

    // construct a Scanner object for getting input
    Scanner sc = new Scanner(System.in);

    System.out.println("Do you wish to continue? Y/N");
    String choice = " ";

    do{
    choice = sc.next();

    if(choice.equalsIgnoreCase("Y")){
        choice="Y";
        choiceSelector = true;
    }
    if(choice.equalsIgnoreCase("N")){
        choice="N";
        choiceSelector = true;
    } else
        choice = sc.next("Please enter a correct value.");

    } while(choiceSelector == false);

    return choice;
}//end getChoice

可能放另一个else ,因为即使输入最初也是Y ,您仍然会要求他们提供正确的值:

if(choice.equalsIgnoreCase("Y")){
    choice="Y";
    choiceSelector = true;
} else if(choice.equalsIgnoreCase("N"))...

和休息

choice = sc.next("Please enter a correct value."); // not a valid pattern provided as an input here

作为

System.out.println("Please enter a correct value.");
choice = sc.next();

return choiceSelector; // the boolean value

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM