Scanner中next()与nextLine()的区别?

mengbingqingcui mengbingqingcui 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
import java.util.*;
class NextNextLine{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("input the first number:");
int a=sc.nextInt();
if(a==-1){
System.out.println("over!");
break;
}
System.out.println("input the operator:");
String s=sc.next();//String s=sc.nextLine()//为什么这个地方用nextLine()就不行了呢?
System.out.println("input the second number:");
int b=sc.nextInt();
if(s.equals("+")){
System.out.println(a+"+"+b+"="+(a+b));
}else if(s.equals("-")){
System.out.println(a+"-"+b+"="+(a-b));
}else if(s.equals("*")){
System.out.println(a+"*"+b+"="+(a*b));
}else if(s.equals("/")){
System.out.println(a+"/"+b+"="+(double)(a/b));
}else{
System.out.println("false!");
}

}
}
}
...全文
评论 30
收藏 已收藏
分享
返回顶部
mengbingqingcui
Java SE 62,620 成员 307,251 内容