System.in System.out

System.in

主要是作為鍵盤輸入串流。

透過getClass(),可以知道System.in的執行類型是BufferedInputStream。

1
System.out.println(System.in.getClass());
class java.io.BufferedInputStream

Scanner

Scanner掃描器,它的建構子參數是System.in鍵盤輸入串流。

1
Scanner scanner = new Scanner(System.in);

Scanner會去鍵盤輸入串流中取得資料。

使用next()方法取得字串。

1
2
3
4
5
6
7
8
public class Test {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Please input:");
    String next = scanner.next();
    System.out.println(next);
  }
}

使用nextInt(),取得數字。

1
int next1 = scanner.nextInt();

System.out

執行類型是PrintStream,將資料顯示在螢幕。

1
System.out.println(System.out.getClass());
class java.io.PrintStream

results matching ""

    No results matching ""