java中输入两个整数,再输入一个符号,根据操作符来判断运算方式,用switch-case嵌套实现。求源代码

如题所述

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("输入有误");
}
}
给好评吧!谢谢
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答