簡體   English   中英

java Scanner類的nextLine()問題

[英]nextLine() Issues with java Scanner Class

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格式化問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM