import java.util.Scanner;
1.next()、nextLine()、nextInt() 的相同点:
next()、nextLine()、nextInt()是scanner内置的方法,遇到 空格,Tab,Enter,会继续等待,直到获得相应类型相应的值。而nextLine() 会把空格,Tab,Enter 视为输入。
2.next()、nextLine()、nextInt()的区别
1.使用 next() 方法时,将空格看作 是两个字符串的 间隔,返回值是字符串类型的。
2.使用 nextLine() 方法时,不将空格看做 是两个字符串的 间隔,而是看作字符串的一部分,返回时,它作为String类型一并返回
3.使用 nextInt() 方法时,与 next() 方法类似,依旧 将空格看作 是两个输入的数据的 间隔,只是它的返回值是 int类型 的,
(当使用nexInt()方法时,只能输入int类型的数据。)
import java.util.Scanner;
/**
* @author 草原一只鹰
* @create 2022-05-12 11:32
*/
public class Main {
public static void main(String[] args) {
//测试哪种方法打开哪种方法的注释即可
//1.测试next()方法
//String next = inputTestnext();
//将输入的字符串进行输出,遇到空格,Tab,Enter,会继续等待,直到获得相应类型相应的值
//System.out.println(next);
//2.测试nextLine()方法
//String nextline = inputTestnextLine();
//System.out.println(nextline);
//3.测试nextInt()方法
int nextInt = inputTestnextInt();
System.out.println(nextInt);
}
public static String inputTestnext() {
Scanner sc = new Scanner(System.in);
//假如输入字符串: a [空格一下] 1 -> 输出:a1
String next = sc.next();
return next;
}
public static String inputTestnextLine() {
Scanner sc = new Scanner(System.in);
//假如输入字符串: b [空格一下] 2 -> 输出:b 空格 2
String nextline = sc.nextLine();
return nextline;
}
public static int inputTestnextInt() {
Scanner sc = new Scanner(System.in);
//假如输入字符串: [空格一下] 2 -> 输出:2
int nextInt = sc.nextInt();
return nextInt;
}
}
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
str = reader.readLine();从这个缓存中读取一行的内容
详解:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
InputStream in = System.in ;
InputStreamReader isr = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(isr);