实现两个数之间的加,减,乘,除
package com.wu.method;
import java.util.Scanner;
public class TestDemo {
//实现两个数之间的加,减,乘,除!!!
public static void main(String[] args) {
//new一个scanner交互对象
Scanner scanner = new Scanner(System.in);
System.out.println("输入你要计算的数字1:");
double num1 = scanner.nextDouble();
System.out.println("输入你要进行什么运算:");
String operator = scanner.next();
System.out.println("输入你要计算的数字2:");
double num2 = scanner.nextDouble();
//赋值给result
double result = 0;
//用switch结构判断加,减,乘,除!!!!!!
switch (operator){
case "+":
result = num1+num2;
break;
case "-":
result = num1-num2;
break;
case "*":
result = num1*num2;
break;
case "/":
result = num1/num2;
break;
default:
System.out.println("输入不合法");
}
System.out.println("你要计算的式子为"+num1+operator+num2+"="+result);
scanner.close();
}
}