Java 中的 for 循环在接受下一个输入之前运行 3 次 [英] for loop in Java runs 3 times before taking next input

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

问题描述

我是 Java 新手,正在学习它.我正在尝试编写代码来实际查看 for-each 循环在 Java 中的工作原理.但我在这方面遇到了问题.我的代码很简单:

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

我不明白为什么 for 循环在接受用户的另一个输入之前要运行三次?

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

有什么帮助吗?提前致谢.

Any help? Thank you in advance.

推荐答案

我假设您使用的是 Windows,因为这种行为似乎适合 Windows.

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. a
  2. 返回

第 2 步传递一个行分隔符序列,向输入控制台指示您已按下 Enter,这告诉控制台您已完成输入该行的输入.所以控制台将整行传递给您的程序.

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.

Windows 上的此行分隔符序列由回车符 ' ' 后跟换行符 ' ' 组成.所以你的台词是真的 "a ".

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

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

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
" // You only entered 'a', the other two characters are from the enter key
array[0] = System.in.next(); // sets array[0] to 'a', "
" are still "waiting" on the input stream

Output: Enter Character 1
> String "
" is still on input, so read from those:
array[1] = System.in.next(); // sets array[1] to '
', "
" is still "waiting"

Output: Enter Character 2
> String "
" is still on input, so read from that
array[2] = System.in.next(); // sets array[2] to '
', 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

你要做的是做一个Scanner,一次读取一整行,所以你处理的唯一字符就是你输入的内容:

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[]

如果您要打印数组中每个元素的整数值,您实际上会看到控制台输出空行的数字.这些数字是 ' '' ' 字符的 ASCII 数字等价物.

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 ' ' and ' ' characters.

有趣的是,我相信如果你在 *nix 系统上运行它,你只会跳过一行而不是两行.这是因为 *nix 系统只使用 ' ' 作为它们的行分隔符,所以在你的行尾只有一个额外的字符.

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 ' ' as their line separator, so you'd only have a single extra character at the end of your line.

另外,我不完全确定为什么你的控制台为 ' '' ' 输出一行;可能是 Java 在内部为两个字符换行,而 Windows 没有.因此,如果您尝试在 Windows shell 中运行它,您甚至可能会得到不同的结果.

Also, I'm not entirely sure why your console is outputting a line for both ' ' and ' '; 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 中的 for 循环在接受下一个输入之前运行 3 次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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