【问题标题】:Beginner trouble do...while loop初学者麻烦做...while循环
【发布时间】:2013-09-13 12:06:30
【问题描述】:

我对 Java 非常陌生,但对 VB.NET 有“一点”经验,这要宽容得多。所以请原谅这可能是一个愚蠢的问题,也请原谅我糟糕的 Java 词汇量。 这是控制台应用程序的 sn-p:

do
{
    out.println("Please enter the name of your space bug and press <enter> to continue");
    out.println();
    bugOne.setName(keyInput.nextLine());

    out.println("You have entered '" + bugOne.getName() + "'.");
    out.println("Is this correct? (y/n)");

    confirmName = keyInput.findWithinHorizon(".",0).charAt(0);
}
while(confirmName != 'y' && confirmName != 'Y');

在第一次迭代时很好,输入的名称会显示出来。然后它进入确认状态,如果我输入“y”或“Y”,它会进入下一行代码(如您所料)。

但是,如果我键入其他任何内容,当它通过第二次迭代时,它不会暂停键盘输入。在我输入确认名称请求后,控制台窗口中生成的行立即读取

You have entered "".
Is this correct(y/n)?

为什么不暂停键盘输入? (在 VB 控制台应用程序中,您必须指定何时要暂停用户输入,但从我在 Java 中看到的情况来看,您没有或不能……或者您可以吗?)

为什么我通过访问器方法 setName 设置的“name”变量在第二次迭代时自动赋值为空?

【问题讨论】:

    标签: java console while-loop do-while


    【解决方案1】:

    要从控制台读取字符/字符串,您可以执行以下操作(取自 here):

    try{
            BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
            String s = bufferRead.readLine();
    
            System.out.println(s);
        }
    

    也不能用==!=操作符比较字符串;使用字符串的equalsequalsIgnoreCase

    请看这里http://www.tutorialspoint.com/java/java_string_equalsignorecase.htm

    【讨论】:

    • 我想我应该提到 keyInput 是扫描仪的一个实例(输入)。但是人们可能通过看到对 nextLine() 的调用就知道了。
    【解决方案2】:

    那是因为当你这样做时

    confirmName = keyInput.findWithinHorizon(".",0).charAt(0);
    

    您正在使用模式 (.) 来告诉扫描仪查找下一个字符。但是,一旦用户输入响应,就会有多个字符(即,有 'n' 和换行符)。但是 findWithinHorizo​​n 并没有选择那个换行符,所以当它回到

    bugOne.setName(keyInput.nextLine());
    

    它会立即拾取留在那里的空的新行。要解决此问题,您只需在调用 findWithinHorizo​​n 之后添加以下代码行:

    keyInput.nextLine();
    

    所以它看起来像这样:

    confirmName = keyInput.findWithinHorizon(".",0).charAt(0);
    keyInput.nextLine();
    

    但实际上,这样做有什么害处?忘记模式匹配,只需将其作为字符串读入即可。

    String confirmName;
    do
    {
        out.println("Please enter the name of your space bug and press <enter> to continue");
        out.println();
        bugOne.setName(keyInput.nextLine());
    
        out.println("You have entered '" + bugOne.getName() + "'.");
        out.println("Is this correct? (y/n)");
    
        confirmName = keyInput.nextLine();
    }
    while(!confirmName.equalsIgnoreCase("y"));
    

    【讨论】:

    • 好的,谢谢。那讲得通。老实说,我不知道为什么我使用char 类型而不是字符串作为confirmName 变量,这只是我在某个地方读到的东西。如果我使用的是 VB,我会一直使用字符串比较或类似的东西。对不起,如果这是一个愚蠢的问题,但“!”是什么?在 while(!confirmName.equalsIgnoreCase("y")) 中,我可以将多个值传递给 equalsIgnoreCase 以便它处理大写和小写,还是我需要将 .ToLower 与 nextLine() 方法一起使用?可以将 ToLower 附加到该方法的末尾吗?
    • @user2774915 confirmName.equalsIgnoreCase("y") 返回confirmName 是否等于“y”或“Y”,就像方法名称所暗示的那样,它不关心两个字符串是大还是小。另一方面,confirmName.equals("y") 仅在用户输入小写“y”时才返回 true。 !(阅读:不是)是否定运算符,就像你做的 a != b(或在 VB 中 a &lt;&gt; b)一样,! 反转布尔值,所以整个表达式只有当用户输入的不等于“Y”或“y”。是的,您可以使用!confirmName.toLower().equals("y"),但没有必要。
    • 非常感谢。我以为那是什么!是为了但只是想确定一下。 equals 方法可以接受多个值吗?说在另一种情况下,我可以有一个 !confirmWhatever.equalsIgnoreCase("red" || "blue")
    • @user2774915 不,它只需要one string,所以你必须这样做!(confirmWhatever.equalsIgnoreCase("red") || confirmWhatever.equalsIgnoreCase("blue"))
    猜你喜欢
    • 2015-03-09
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2013-09-26
    相关资源
    最近更新 更多