java的输入用Scanner类
1)单个输入:
例如:
import java.util.Scanner;
public class AA {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();// 输入1
System.out.println(a);// 输出1
}
}
2)无限输入:
例如:
import java.util.Scanner;
public class AA {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int a = sc.nextInt();
System.out.println(a);
}
}
}
3)输入一个整数再输入一个字符:(java没用单独输入一个字符的语句,全都定义为字符串)
例如1):
import java.util.Scanner;
public class AA {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();// 输入1
String b = sc.next();// 输入2
System.out.println("a:"+a+"--b:"+b);// 输出a:1--b:2
}
}
例如2):
import java.util.Scanner;
public class AA {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();// 输入1
sc.nextLine();// 接收回车符,这里只能用sc.nextLine();
String b = sc.nextLine();// 输入2
System.out.println("a:"+a+"--b:"+b);// 输出a:1--b:2
}
}