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;
用前需要引入import java.io.Reader;
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) { } } }🎜更多Java輸入流Scanner/BufferedReader使用方法範例相關文章請關注PHP中文網! 🎜