Java輸出流Scanner/BufferedReader應用辦法示例。本站提示廣大學習愛好者:(Java輸出流Scanner/BufferedReader應用辦法示例)文章只能為提供參考,不一定能成為您想要的結果。以下是Java輸出流Scanner/BufferedReader應用辦法示例正文
1.應用Scanner
應用時須要引入包import java.util.Scanner;起首界說Scanner對象
Scanner sc = new Scanner(System.in);
假如要輸出整數,則 int n = sc.nextInt();
String類型的,則String temp = sc.next();
好比:
import java.util.Scanner;
public class Test {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(scanner.next());
}
}
2.應用BufferedReader
用前須要引入 import java.io.Reader;
BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
String input = br.readLine();
好比:
import java.io.*;
public class Test {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out.println(br.readLine());
} catch (IOException e) {
}
}
}