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物件才有。