java控制台输入语句:
Scanner sc = new Scanner(System.in);
通过一个变量,例如 int r; r = sc.nextInt();
例子:
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("输入第一个boolean值(true/false):");
- if(sc.nextBoolean()){
- System.out.println("输入布尔:真的");
- }else{
- System.out.println("输入布尔:假的");
- }
- System.out.println("输入第一个数字:");
- System.out.println("输入数字:"+sc.nextInt());
- System.out.println("输入一个字符串:");
- System.out.println("输入字符串:"+sc.next());
- System.out.println("输入一个长整型:");
- System.out.println("输入长整型:"+sc.nextLong());
- }
接收多行语句的方法:
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] strs = new String[n];
for(int i = 0; i < n; i++){
strs[i] = sc.next();
}
sc.close();