• next()与nextLine()的区别


     1 abc def ghij
     2 kl mno pqr st
     3 uvw xyz
     4 你用next(),第一次取的是abc,第二次取的是def,第三次取的是ghij
     5 你用nextLine(),第一次取的是abc def ghij,第二次取的是kl mno pqr st,第三次取的是uvw xyz
     6 明白了吧。前一个是以回车或空格为分隔符,一次取一个单词,后一个是以回车为分隔符,一次取一行。
     7 
     8 nextLint()一般用于IO的读取,表示获取下一行数据
     9 next()表示返回迭代的下一个元素。
    10         Scanner scan = new Scanner(System.in); 
    11         // 从键盘接收数据  
    12      
    13         //nextLine方式接收字符串
    14             System.out.println("nextLine方式接收:");
    15             // 判断是否还有输入
    16             if(scan.hasNextLine()){   
    17               System.out.println("这是nextLine():");
    18               String str2 = scan.nextLine();
    19               System.out.println("输入的数据为:"+str2); 
    20               System.out.println("这是next():");
    21               String str3 = scan.next();
    22               System.out.println("输出的内容为:" + str3);
    23             }  
  • 相关阅读:
    enum
    爬虫实战2:爬头条网美图--Ajax图片加载处理
    爬虫实战1:使用requests和正则爬取电影信息
    爬虫7:selenium
    爬虫6:pyquery库
    爬虫5:beautifulsoup
    爬虫4:re库
    爬虫3:requests库
    爬虫2:urllib
    爬虫1:概述
  • 原文地址:https://www.cnblogs.com/www-yang-com/p/7993397.html
Copyright © 2020-2023  润新知