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()应该可以解决此问题。

最佳答案

运行此命令时,我得到以下信息:

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


如预期的那样。

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

10-07 19:34
查看更多