简体   繁体   English

Java输入缓冲区和do-while循环行为(为什么它会检查第一个字符3次?)

[英]Java input buffer and do-while loop behavior (why does it check first character 3 times?)

I have been learning Java with Oracle's “Java A Beginner's Guide”, and was having trouble understanding an example program from the book about the input buffer and do-while loop. 我一直在使用Oracle的“Java A初学者指南”学习Java,并且无法理解书中关于输入缓冲区和do-while循环的示例程序。

This example is about a number guessing program. 这个例子是一个猜测程序。 It's designed to ignore any extra characters you might enter. 它旨在忽略您可能输入的任何额外字符。 So if you enter ABC then push enter it will only check A. 因此,如果您输入ABC然后按输入它将只检查A.

My question is more about the input buffer, if I comment out the 2nd do-while loop that deals with ignore then run the program, it checks that single character 3 times. 我的问题更多的是关于输入缓冲区,如果我注释掉处理忽略的第二个do-while循环然后运行程序,它会检查该单个字符3次。 So if I enter a single 'A' then push enter it appears to check 3 times and I get the “...Sorry, you're too low” output blurb 3 times. 因此,如果我输入单个'A'然后按下输入它似乎检查3次,我得到“...抱歉,你太低了”输出模糊3次。

If I enter 'AZ' then push enter it seems to check 4 times, it checks A then Z then A 2 more times. 如果我输入'AZ'然后按下输入它似乎检查4次,它检查A然后Z然后再检测A 2次。


Questions: 问题:

  1. Why does it seem to always check the first character 3 times? 为什么它似乎总是检查第一个字符3次?
  2. Does the enter key assign \\n to the ignore variable? enter键是否为ignore变量赋值?
    When the 2nd do-while loop is not commented out, is this the correct sequence if you enter ABC then push enter? 当没有注释掉第二个do-while循环时,如果你输入ABC然后按回车,这是正确的顺序吗?
    A – assigned to ch A - 分配给ch
    B- assigned to ignore B-分配忽略
    C- assigned to ignore C-分配忽略
    Enter key (\\n) – assigned to ignore, loop exits because ignore is assigned \\n 输入键(\\ n) - 指定为忽略,循环退出,因为忽略了指定\\ n

I am a bit confused if the enter key actually assigns \\n to ignore? 如果输入键实际指定\\ n忽略,我有点困惑? Or is it just waiting until there are no more characters to assign to ignore? 或者只是等到没有更多的字符要指定忽略? The book says pushing enter causes a newline. 这本书说推进入会导致换行。 So that do-while loop is terminated when: (ignore != '\\n'). 因此do-while循环在以下时间终止:(忽略!='\\ n')。

Thanks for any help! 谢谢你的帮助!

class Guess4{
    public static void main (String args[])
        throws java.io.IOException {

            char ch, ignore, answer = 'K';

            do {
                System.out.println("I'm thinking of a letter between A and Z.");
                System.out.println("Can you guess it: ");

                //read a character
                ch = (char) System.in.read();

                /* discard any other characters in the input buffer
                do {
                    ignore = (char) System.in.read();
                } while(ignore != '\n');
                */

                if(ch == answer) System.out.println("**Right**");
                else {
                    System.out.print("...Sorry, you're ");
                    if(ch < answer) System.out.println("too low");
                    else System.out.println("too high");
                    System.out.println("Try again!\n");
                }
            } while(answer != ch);
        }
}

Question 1: 问题1:

(As I already wrote in my comment, I did not get 3 lines if I entered just one character, I got 2, which is correct behavior) (正如我在评论中写的那样,如果我只输入一个字符,我没有得到3行,我得到2,这是正确的行为)

Your assumption that the first character is checked multiple times is wrong. 您多次检查第一个字符的假设是错误的。 For simple debugging, print the character that will be tested: 对于简单的调试,打印将要测试的字符:

// ...
System.out.println("Now checking: '" + ch + "''");
if(ch == answer) System.out.println("**Right**");
// ...

Now, when you enter abc[enter] , you will see that each character is processed individually and each of them causes the same message. 现在,当您输入abc[enter] ,您将看到每个字符都被单独处理,并且每个字符都会产生相同的消息。 Also the newline is tested. 新线也经过测试。

Question 2: 问题2:

You're absolutely right. 你是绝对正确的。

There is one important thing though: The whole point of the second do-while construct is to clear the input-stream for your next guess! 但有一件重要的事情:第二个do-while构造的重点是清除输入流以供下次猜测! That's what the comment above at least tries to point out: discard any other characters in the input buffer . 这就是上面的评论至少试图指出: discard any other characters in the input buffer Checking for the newline is just so you know you can exit the loop because one guess is limited to one line! 检查换行符只是让你知道你可以退出循环,因为一个猜测仅限于一行! Actually you don't even need that ignore variable at all. 实际上你根本不需要忽略变量。 The character is removed from the stream by reading it. 通过读取该字符将从流中删除。 So it's about the reading itself by calling System.in.read() - you don't have to assign the result to anything. 所以它是通过调用System.in.read()读取本身 - 您不必将结果分配给任何东西。

You could replace the second do-while loop with just do {} while(System.in.read() != '\\n'); 你可以用do {} while(System.in.read() != '\\n');替换第二个do-while循环do {} while(System.in.read() != '\\n'); .

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

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