Given the code fragment:

public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter GDP: ");
    //line 1
}

Which code fragment, when inserted at line 1, enables the code to read the GDP from the user?

A.

int GDP = Integer.parseInt(br.readLine());
B.
int GDP = br.read();
C.
int GDP = br.nextInt();
D.
int GDP = Integer.parseInt(br.next());

題解

選項 A,從標準輸入串流讀取一行字串之後,轉成整數儲存。

選項 B,BufferedReader 物件的 read 方法會讀取一個字元,接著回傳字元的值,不是題目想要的結果。

選項 C,BufferedReader 物件沒有 nextInt 方法,會編譯錯誤。Scanner 物件才有。

選項 D,BufferedReader 物件沒有 next 方法,會編譯錯誤。Scanner 物件才有。