zoukankan      html  css  js  c++  java
  • next(),nextLine(),nextInt的区别

    next()

    next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab键或Enter键等结束符,next()方法会自动将其去掉,只有在输入有效字符之后,next()方法才将其后输入的空格键、Tab键或Enter键等视为分隔符或结束符。

    读取完之后会把缓冲区中的“ ”删除掉。

    nextInt()

    nextInt()方法在扫描到空白符的时候会将前面的数据读取走,但会丢下空白符“ ”在缓冲区中,但是,nextLine()方法在扫描的时候会将扫描到的空白符一同清理掉。

    nextLine()

    nextLine()方法的结束符只是Enter键,即nextLine()方法返回的是Enter键之前的所有字符。读取完之后会把缓冲区中的“ ”删除掉。

     System.out.println("请输入第一个字符串");
            String str1 = sc.next();
            System.out.println("请输入第二个字符串");
            String str2 = sc.next();
            System.out.println(str1);
            System.out.println(str2);
            System.out.println(str1+str2);

    str2会被跳过输入,得到“r”

    System.out.println("请输入第三个字符串");
            String str3 = sc.nextLine();
            System.out.println("请输入第四个字符串");
            String str4 = sc.nextLine();
            System.out.println(str3);
            System.out.println(str4);
            System.out.println(str3+str4);

    next Line()会读取“ ”回车键之前的所有数据,包括空格键。并最后把“ ”从缓冲区中删除。

    System.out.println("请输入第三个整数");
            int str3 = sc.nextInt();
            System.out.println("请输入第四个字符串");
            String str4 = sc.nextLine();
            System.out.println("请输入第五个字符串");
            String str5 = sc.next();
            System.out.println(str3);
            System.out.println(str4);
            System.out.println(str5);
            System.out.println(str3+str4+str5);

    nextInt()读取整数后,会在缓冲区留下“ ”,str4中无数据,str5为qwe(提示键入值)

  • 相关阅读:
    Appium自动化环境搭建
    真机Android 8.0版本以上uiautomator定位元素-Unsupported protocol: 2/Unexpected error while obtaining UI hierarchy错误处理
    rsa非对称加密
    QT使用OpenSSL的接口实现RSA的加密解密
    lua安装后其他库使用产生问题解决方法
    log4cpp的使用描述
    std::function和std::bind
    C++11线程睡眠的方式
    高精度计时器
    如何解决TCP拆包粘包问题
  • 原文地址:https://www.cnblogs.com/chenfx/p/13430755.html
Copyright © 2011-2022 走看看