Java zero-based learning, 2020-11-24

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

  1. You must read a valid character before you can end the input
  2. For the blanks encountered before the input valid characters, the next() method will automatically remove them
  3. Only after valid characters are entered, the blanks entered after them are used as separators or end characters
  4. next() cannot get a string with spaces
System.out.println("请输入数据:");
        String str=scanner.nextLine();
        System.out.println("输出内容为:"+str);

*nextLine()* Usage

  1. Use carriage return as the terminator, that is, the nextLine() method returns all the characters before the carriage return.
  2. 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);//输出数据

Guess you like

Origin blog.csdn.net/qq_52044923/article/details/110085872