在Java中循环运行3次,然后再进行下一次输入 [英] for loop in Java runs 3 times before taking next input

查看:543
本文介绍了在Java中循环运行3次,然后再进行下一次输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Java和学习它。我正在尝试一个代码来真正看到for-each循环如何在Java中工作。但是我遇到了一个问题。我的代码很简单:

  class ForEach 
{
public static void main(String [] args)抛出java.io.IOException
{
char [] array = new char [10]; (int i = 0; i <10; i ++)



System.out.println(Enter Character+ i);
array [i] =(char)System.in.read();


System.out.println(显示数组中的字符); (char i:array)


{
System.out.println(i);
}
}
}

但输出很尴尬。我相信我的代码有问题。我不能找到它。
这段代码的输出是:

$ $ p $ $ $ $ $ $ $输入字符0
a
输入字符1
输入字符2
输入字符3
b
输入字符4
输入字符5
输入字符6
c
输入字符7
输入字符8
输入字符9
d
显示数组中的字符:
a


b


c


d

我不明白为什么是循环运行三次之前,它需要从用户的另一个输入?



任何帮助吗?感谢您提前。

解决方案

我假设你在Windows上,因为这种行为似乎正确的Windows。 / b>

当你输入一个字符时,你可以这样做:


  1. 按<



  2. 返回
  3. 步骤2传递一个行分隔符序列,向输入控制台指示您按下的输入,这将告诉控制台您已经完成输入这一行。所以控制台将整行传递给你的程序。

    这个在Windows上的分隔符序列由回车符'\r '后跟换行符'\ n'。所以你的这一行是真的 a \r\\\



    什么 System.in.read()是从输入控制台读取一个字节。由于你输入的字符都适合单个字节,你的程序所做的就是逐个消耗输入的字符,所以就像这样:

     输出:输入字符0 
    >用户输入:a \r\\\
    //您只输入'a',另外两个字符来自输入键
    array [0] = System.in.next(); //将数组[0]设置为'a',输入流'\r\\\
    '仍然等待输入流

    输出:输入字符1
    >字符串\r\\\
    仍然是输入,所以阅读这些:
    array [1] = System.in.next(); //将数组[1]设置为'\r',\ n仍然是等待

    输出:输入字符2
    >字符串\ n仍然是输入,所以从
    array [2] = System.in.next(); //将数组[2]设置为'\ n',输入源为空

    输出:输入字符3
    >程序等待用户输入

    ...等等

    你要做的是做一个扫描器,并且一次读完整行,所以你处理的唯一字符就是你输入的内容:

     扫描仪扫描仪=新扫描仪(System.in); //创建一个从System.in 
    读取的新扫描器String line = scanner.nextLine(); //从控制台读取整行,**不在行尾添加换行符**
    //使用line.charAt(i)获取单个字符,或者使用line.toCharArray()获取字符串的支持char []

    如果要打印每个元素的整数值数组,你实际上会看到你的控制台输出空白行的数字。这些数字是'\ r''\ n'字符的ASCII数字等值。有趣的是,我相信如果你是在一个* nix系统上运行的话,你只需要跳过一行而不是两行。这是因为* nix系统只使用'\ n'作为行分隔符,所以在行尾只有一个额外的字符。另外,我还不完全确定为什么你的控制台输出一行'\r' $ b '\\\
    '
    ;可能是Java在内部为两个字符分行,而Windows则不行。所以你甚至可能得到不同的结果,如果你想在Windows shell中运行这个。


    I am new to Java and learning it. I was trying out a code to actually see how the for-each loop works in Java. But I faced a problem regarding that. My code is simple :

    class ForEach
    {
    public static void main(String[] args) throws java.io.IOException
    {
        char[] array = new char[10];
    
        for(int i = 0; i < 10; i++)
        {
            System.out.println("Enter Character " + i);
            array[i] = (char)System.in.read();
        }
    
        System.out.println("Displaying characters in array : ");
    
        for(char i : array)
        {
            System.out.println(i);
        }
    }
    }
    

    but the output is awkward. I am sure something is wrong with my code. I can't just find it. The output of this code is :

    Enter Character 0
    a
    Enter Character 1
    Enter Character 2
    Enter Character 3
    b
    Enter Character 4
    Enter Character 5
    Enter Character 6
    c
    Enter Character 7
    Enter Character 8
    Enter Character 9
    d
    Displaying characters in array :
    a
    
    
    b
    
    
    c
    
    
    d
    

    I do not understand why is the for loop running thrice before it takes another input from user?

    Any help? Thank you in advance.

    解决方案

    I'm assuming you're on Windows, because this behavior seems about right for Windows.

    When you enter a character, you do something like this:

    1. Press a
    2. Press return

    Step 2 passes a line separator sequence that indicates to the input console that you have pressed enter, which tells the console that you have finished entering this line of input. So the console passes the entire line to your program.

    This line separator sequence on Windows is made up of the carriage return character '\r' followed by the newline character '\n'. So your line is really "a\r\n".

    What System.in.read() does is read a single byte from the input console. As the characters you input all fit into a single byte, what your program does is consumes the inputted characters one by one, so it's something like this:

    Output: Enter Character 0
    > User inputs: "a\r\n" // You only entered 'a', the other two characters are from the enter key
    array[0] = System.in.next(); // sets array[0] to 'a', "\r\n" are still "waiting" on the input stream
    
    Output: Enter Character 1
    > String "\r\n" is still on input, so read from those:
    array[1] = System.in.next(); // sets array[1] to '\r', "\n" is still "waiting"
    
    Output: Enter Character 2
    > String "\n" is still on input, so read from that
    array[2] = System.in.next(); // sets array[2] to '\n', input source is now empty
    
    Output: Enter Character 3
    > Nothing is waiting on the input stream, so program waits for user input
    
    ... and so on
    

    What you want to do is make a Scanner, and read an entire line at once, so the only character you process is the content you entered:

    Scanner scanner = new Scanner(System.in); // Creates a new scanner that reads from System.in
    String line = scanner.nextLine(); // Reads an entire line from console, **not including newline breaks at the end of a line**
    // use line.charAt(i) to get individual characters, or use line.toCharArray() to get the string's backing char[]
    

    If you were to print the integer values of each of the elements in your array, you'd actually see numbers where your console outputs blank lines. These numbers are the ASCII numerical equivalents to the '\r' and '\n' characters.

    Interestingly, I believe that if you were run this on a *nix system you'd only skip a single line instead of two. This is because *nix systems use just '\n' as their line separator, so you'd only have a single extra character at the end of your line.

    Also, I'm not entirely sure why your console is outputting a line for both '\r' and '\n'; could be that Java breaks lines for both characters internally, while Windows does not. So you might even get different results if you were to try to run this in a Windows shell.

    这篇关于在Java中循环运行3次,然后再进行下一次输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆