Java利用Scanner 加Swich实现计算器
package base;
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
Scanner a=new Scanner(System.in);
double sum = 0;
System.out.println("请输入第一个数字:");
double num1=a.nextDouble();
System.out.println("请输入第二个数字:");
double num2=a.nextDouble();
System.out.println("请输入运算符:");
String op=a.next();
switch (op){
case "+":
sum=num1+num2;
System.out.println(num1+"+"+num2+"="+sum);
break;
case "-":
sum=num1-num2;
System.out.println(num1+"-"+num2+"="+sum);
break;
case "*":
sum=num1*num2;
System.out.println(num1+"*"+num2+"="+sum);
break;
case "/":
sum=num1/num2;
System.out.println(num1+"/"+num2+"="+sum);
break;
}
}
}
xinyue Day2