java中输入两个整数,再输入一个符号,根据操作符来判断运算方式,用switch-case嵌套实现。求源代码
1个回答
2013-08-15
展开全部
while(true){
System.out.print("请输入:例如 1+2,4/6..");
Scanner s = new Scanner(System.in);
String str = s.next();
if(str.length()==3){
try {
float a = Float.parseFloat(str.substring(0,1));
float b = Float.parseFloat(str.substring(2,3));
String c = str.substring(1,2);
switch (c.hashCode()) {
case 43:
System.out.println(a+"+"+b+"="+(a+b));
break;
case 45:
System.out.println(a+"+"+b+"="+(a-b));
break;
case 42:
System.out.println(a+"+"+b+"="+(a*b));
break;
case 47:
System.out.println(a+"+"+b+"="+(a/b));
break;
default:
break;
}
} catch (Exception e) {
System.out.println("输入有误");
}
}else{
System.out.println("输入有误");
}
}
给好评吧!谢谢
System.out.print("请输入:例如 1+2,4/6..");
Scanner s = new Scanner(System.in);
String str = s.next();
if(str.length()==3){
try {
float a = Float.parseFloat(str.substring(0,1));
float b = Float.parseFloat(str.substring(2,3));
String c = str.substring(1,2);
switch (c.hashCode()) {
case 43:
System.out.println(a+"+"+b+"="+(a+b));
break;
case 45:
System.out.println(a+"+"+b+"="+(a-b));
break;
case 42:
System.out.println(a+"+"+b+"="+(a*b));
break;
case 47:
System.out.println(a+"+"+b+"="+(a/b));
break;
default:
break;
}
} catch (Exception e) {
System.out.println("输入有误");
}
}else{
System.out.println("输入有误");
}
}
给好评吧!谢谢
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询