java实现循环一次增加一天,在Java中循环运行3次,然后再进行下一次输入

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:

Press a

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值