简体   繁体   English

不知道为什么我的 for 循环没有按照我的预期工作

[英]Not sure why my for loop isn't working the way i intended it to

So, before I start I just wanted to say that I'm very new to Java as a language and I've been reading a text book that was recommended to me.所以,在我开始之前,我只想说我对 Java 作为一门语言非常陌生,而且我一直在阅读一本推荐给我的教科书。 One of the examples provided within the text book on for loops had the following code, which is meant to generate an infinite for loop until the user presses the character 'S' on their keyboard.教科书中提供的有关 for 循环的示例之一具有以下代码,该代码旨在生成无限的 for 循环,直到用户按下键盘上的字符“S”。

Here is the code:这是代码:

class ForTest {
    public static void main(String args[])
        throws java.io.IOException {
            int i;
            System.out.println("Press S to stop.");
            for (i = 0; (char) System.in.read() != 'S'; i++) 
                System.out.println("Pass #" + i);
                
        }
}

I copied the code exactly as it was written within the book but when I run the program, to my surprise, it doesn't start printing out numbers onto the console.我完全按照书中的内容复制了代码,但是当我运行程序时,令我惊讶的是,它并没有开始在控制台上打印出数字。 Additionally, whenever I press any keys on the keyboard it generates three numbers within the sequence.此外,每当我按下键盘上的任何键时,它都会在序列中生成三个数字。 Example shown below:示例如下所示: 在此处输入图像描述

I have also included a screenshot of the code from the book below:我还附上了下面书中代码的截图: 在此处输入图像描述

I was wondering whether anyone knows why this is the case.我想知道是否有人知道为什么会这样。 Any help would be greatly appreciated thanks.任何帮助将不胜感激,谢谢。

The reason it's printing multiple times is because multiple characters are detected.它多次打印的原因是检测到多个字符。 In your case, it's printing twice because you entered a value ( Pass 1 ) and a new line ( Pass 2 )在您的情况下,它打印两次,因为您输入了一个值( Pass 1 )和一个新行( Pass 2

The problem you have is not with System.in.read(), but because the console is usually using a buffered approach.您遇到的问题不在于 System.in.read(),而是因为控制台通常使用缓冲方法。 Meaning that data is only transferred to the System.in.read() once you press enter.这意味着数据只有在您按下回车后才会传输到System.in.read()

So to get the example working, you would have to switch the console to an unbuffered mode, but there is no portable way to do this, because there are so much different types of consoles.因此,要使示例正常工作,您必须将控制台切换到无缓冲模式,但没有可移植的方式来做到这一点,因为有很多不同类型的控制台。 Maybe have a look at what editor/console the book is using也许看看这本书使用的是什么编辑器/控制台

This block of code looks like it was written by someone who was deliberately trying to make it obtuse and difficult to comprehend for a beginner.这段代码看起来像是由某个人编写的,他故意试图使它变得迟钝且难以让初学者理解。

The middle expression of a for statement is the criterion for taking the next step of the loop. for语句的中间表达式是执行循环下一步的标准。 It is evaluated before each step of the loop to determine whether the for loop is complete yet.它在循环的每个步骤之前进行评估,以确定 for 循环是否完成。 In this case, it calls in.read() and checks if the input is S before each step of the loop.在这种情况下,它调用in.read()并在循环的每一步之前检查输入是否为S

in.read() waits for the next line of input it gets. in.read()等待它获得的下一行输入。 When you enter a value and press Enter, that line gets read, so the loop takes a step.当您输入一个值并按 Enter 键时,将读取该行,因此循环会执行一步。 And a new line is also entered, so the loop takes a second step.并且还输入了一个新行,因此循环进行了第二步。

It will not print lines to the console unless you enter lines, because in.read() causes the program to block (wait) for the next input.除非您输入行,否则它不会将行打印到控制台,因为in.read()会导致程序阻塞(等待)下一个输入。

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

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