nextLine和next的区别
来源:互联网 发布:js判断不等于0 编辑:程序博客网 时间:2024/05/27 09:44
两者的共同作用:都能接收字符串数据
import java.util.Scanner;public class Demo { //next和nextLine的基本用法 public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("输入nextLine的字符串:"); String str2=sc.nextLine(); System.out.println(str2); System.out.println("输入next的字符串:"); String str=sc.next(); System.out.println(str); }}
区别一:next如果前面有空格,Table,Enter都不会识别,直到遇到字符串才开始到遇到空格或者Enter结束
import java.util.Scanner;public class Demo2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("请输入字符串:"); String s=sc.next(); //开头会忽略空格,table,enter //遇到字符串后遇到空格就结束了 System.out.println(s); }}
区别二:nextLine 如果前面有Enter就会结束读取,否则会读取整行内容(包括空格,table),知道遇到Enter结束.
import java.util.Scanner;public class Demo3 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("请输入字符串:"); String s=sc.nextLine(); //如果Enter会结束读取 //会读取整行内容(包括空格,table) System.out.println(s); }}
**附:next和nextLine同时使用时(并且next在前)会出现问题!
原因:nextLine会接收next的enter从而结束读取**
import java.util.Scanner;public class Demo4 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("输入next的字符串:"); String str=sc.next(); System.out.println(str); System.out.println("输入nextLine的字符串:"); String str2=sc.nextLine(); System.out.println(str2); System.out.println("nextLine结束读取!"); }}
**解决方法:
1.重新创建一个键盘录入对象**
import java.util.Scanner;public class Demo4 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("输入next的字符串:"); String str=sc.next(); System.out.println(str); //解决方法一:添加一键盘录入对象 Scanner sc2=new Scanner(System.in); System.out.println("输入nextLine的字符串:"); String str2=sc2.nextLine(); System.out.println(str2); System.out.println("nextLine结束读取!"); }}
2.增加一个nextLine接收Enter
import java.util.Scanner;public class Demo6 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("输入next的字符串:"); String str=sc.next(); System.out.println(str); System.out.println("输入nextLine的字符串:"); sc.nextLine(); //解决方法二:next后面添加一个nextLIne String str2=sc.nextLine(); System.out.println(str2); System.out.println("nextLine结束读取!"); }}
阅读全文
0 0
- next()和nextLine()的区别
- next()和nextLine()的区别
- next()和nextline()的区别
- next()和nextLine()的区别
- next()和nextLine()的区别
- next和nextLine的区别
- next和nextLine的区别
- next()和nextLine()的区别
- next()和nextLine()的区别
- nextLine和next的区别
- next()和nextLine()的区别
- next()和nextLine()区别
- next()和nextline()区别
- nextLine()和next()的区别和使用方法
- nextLine()和next()的区别和使用方法
- Scanner的next()和nextLine()的区别
- java中next()和nextLine()的区别
- sc.next() 和 nextLine 的区别
- Hibernate运行错误之java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getPro
- 【C++面向对象程序设计】20170419分数加减
- 深度学习深理解(五)-改善深度神经网络
- C++字符串常量
- (M)Backtracking:22. Generate Parentheses
- nextLine和next的区别
- make切换到另一目录并执行另一目录makefile
- 【QT】获取时间戳
- TCP/IP协议简介
- deeplearning_LogisticRegressionwithaNeuralNetworkmindset
- 机器翻译(一):BLEU(bilingual evaluation understudy)
- socket
- 表单设计案例——学生入校注册页面设计
- 问题:2006年培养学员八万人,每年增长25%,请问按此增长速度,到哪一年培训学员人数达到20万人?