首页 > 其他 > 详细

Scanner对象 next() 和 nextLine()

时间:2020-04-23 16:56:37      阅读:60      评论:0      收藏:0      [点我收藏+]

Scanner对象 next() 和 nextLine()

next():

? 1.一定要读取到有效字符后才可以结束输入

? 2.对输入有效字符之前遇到的空白,next()方法会自动将其去掉

? 3.只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符

? 4.next()不能得到带有空格的字符串

nextLine():

? 1.以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符

? 2.可以获得空白

public class Demo04 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
       System.out.println("next的方式输入:");
       String next = scanner.next();
       scanner.nextLine(); // 加上这一行就能一直输入了
       System.out.println("next输出的结果为:"+next);

       System.out.println("nextLine的方式输入:");
       String nextLine = scanner.nextLine();
       System.out.println("nextLine输出的结果为:"+nextLine);
    }
}

Scanner对象 next() 和 nextLine()

原文:https://www.cnblogs.com/liqiliang1437/p/12761683.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!