import java.util.Scanner;
public class Calculator{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入数字a:");
double a = sc.nextDouble();
System.out.println("请输入运算符:");
String operator = sc.next();
char opt = operator.charAt(0);
System.out.println("请输入数字b:");
double b = sc.nextDouble();
switch(opt){
case '+':
System.out.println( a+"+"+b+"="+(a+b) );
break;
case '-':
System.out.println( a+"-"+b+"="+(a-b) );
break;
case '*':
System.out.println( a+"*"+b+"="+(a*b) );
break;
case '/':
System.out.println( a+"/"+b+"="+(a/b) );
break;
default:
System.out.println( "该运算符操作尚未定义!" );
}
}
}
Java基础语法之分支结构switch——迷你计算器
最新推荐文章于 2022-01-24 20:18:56 发布