Scanner object
basic syntax: ***Scanner s=new Scanner(System.in);***`
scanner.close//用完scanner就要用这行代码关闭它
Scanner s=new Scanner(System.in);//准备从键盘接收数据
System.out.println("使用next方式接收:");
if(scanner.hasNext()){
String str=scanner.next();//
System.out.println("输出内容为:"+str);
*next():*Usage
- You must read a valid character before you can end the input
- For the blanks encountered before the input valid characters, the next() method will automatically remove them
- Only after valid characters are entered, the blanks entered after them are used as separators or end characters
- next() cannot get a string with spaces
System.out.println("请输入数据:");
String str=scanner.nextLine();
System.out.println("输出内容为:"+str);
*nextLine()* Usage
- Use carriage return as the terminator, that is, the nextLine() method returns all the characters before the carriage return.
- You can get blanks.
In short, next() and nextLine() and scanf() and gets() in the c language have similar usages to
receive data from the keyboard
int i=0;
i=scanner.nextInt();//接收数据
System.out.println("输出内容为:"+i);//输出数据