Rumah >Java >javaTutorial >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) { } } }
更多Java输入流Scanner/BufferedReader使用方法示例相关文章请关注PHP中文网!