Unlike C#, JAVA can directly enter the content through the Console.ReadLine statement, and JAVA needs to guide the package
Proceed as follows:
Guide package: import java.util.Scanner;
Create an object instance: Scanner sc = new Scanner(System.in);
Calling method: int i=sc.nextlnt(); The input used to receive the console is a number
String s=sc.next(); used to receive console input is a string
Related code implementation:
import java.util.Scanner;
public class ScannerDemo01 {
public static void main(String[] args) {
//创建Scanner引用类型的变量
Scanner sc = new Scanner(System.in);
//获取数字
System.out.println("请输入一个数字");
int n = sc.nextInt();
System.out.println("n的值为" + n);
//获取字符串
System.out.println("请输入一个字符串");
String str = sc.next();
System.out.println("str的值为" + str);
}
}
result: