0
import java.util.Scanner;

public class b {

    public static void main(String[] args){

        Scanner keyboard = new Scanner(System.in);
        String[] lines = new String[5];

        lines[0] = keyboard.nextLine();
        lines[1] = keyboard.nextLine();
        lines[2] = keyboard.nextLine();
        lines[3] = keyboard.nextLine();
        lines[4] = keyboard.nextLine();

        for (int i = 0; i < lines.length; i++) {
            System.out.println(lines[i]);
        }
    }
}

为什么上面的代码不允许我存储以下内容:

Hello there
How are you
My name is
Bill Gates
What is yours?

我得到的输出是:

Hello there
How are you
My name is

Process completed.

我知道原语和 nextInt() 存在问题,但我认为 nextLine() 应该解决这个问题。

4

1 回答 1

1

当我运行它时,我得到了这个:

Hello there
How are you
My name is
Bill Gates
What is yours?

正如预期的那样。

检查您没有运行旧版本的程序。还要从命令行测试您的应用程序以排除任何IDE格式问题。

于 2013-01-16T01:03:37.607 回答