nextInt() nextLine next详解

  • nextInt()   它只读取int值,并且遇到空格也停止,在读取输入后将光标放在同一行
  • nextLine()  读取输入,包括单词之间的空格(即,读取到行尾),读取输入后,将光标定位在下一行
  • next()  只读取输入到空格。它不能读两个用空格隔开的单词。另外,在读取输入后将光标放在同一行

举例1

  •                 Scanner cin = new Scanner(System.in);
    		        int str1 = cin.nextInt();
    		        String str2 = cin.nextLine();
    		        System.out.println("end"+str2);
    		        System.out.println("end"+str1);
    		          

    输出结果为:

123
end
end123

证明:nextInt在读取输入后将光标放在同一行

举例2

123 123
end 123
end123

上面同样的代码输入123 123结果

证明:nextInt()遇到空格也停止

举例3

如果想让nextInt()后再继续读取下一行

 Scanner cin = new Scanner(System.in);
		        int str1 = cin.nextInt();
		        cin.nextLine();
		        String str2 = cin.nextLine();
		     
		        System.out.println("end"+str1);
		        System.out.println("end"+str2);
		          
		          
		          }

 

123
123
end123
end123

举例4

 
		      Scanner cin = new Scanner(System.in);  

              String str1=cin.next();
		        String str2=cin.nextLine();
		        
                 System.out.println("end"+str1);
		        
		        System.out.println("end"+str2);

 

结果:

str str
endstr
end str

next()和nextInt()有点类似,不过前者可以读取字符串,它也是空格停止

 

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值