switch选择语句在控制台实现一个简易的计算器(+ - * / %)
public class TestSwitch11 {
public static void main(String args[]){
System.out.println("欢迎使用简单计算器系统:");
Scanner s=new Scanner(System.in);
System.out.print("请输入第一个数字:");
int a=s.nextInt();
System.out.print("请输入运算符:");
String str=s.next();
System.out.print("请输入第二个数字:");
int b=s.nextInt();
System.out.print("运算结果:");
switch (str){
case "+":
System.out.println(a+str+b+"="+(a+b));
break;
case "-":
System.out.println(a+str+b+"="+(a-b));
break;
case "*":
System.out.println(a+str+b+"="+(a*b));
break;
case "/":
System.out.println(a+str+b+"="+(a/b));
break;
case "%":
System.out.println(a+str+b+"="+(a%b));
break;
default:
System.out.println("请输入有效的运算符!!!!!");
}
}
}
输出效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e1c3ba8e8c6698c955c7b68397e78e6f.png)