package com.chao.method;标签:Java,num1,num2,--,double,System,println,method,out From: https://www.cnblogs.com/CaiDingChao/p/17717147.html
import java.util.Scanner;
//加、减、乘、除
/*
键盘输入两个数
选择 + - * / 四个中的一个,对应输出选择运算符计算后的结果
*/
public class Demo05 {
public static void main(String[] args) {
double sum = 0;
double db = 0;
double db1 = 0;
double db2 = 0;
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的第1个数:");
double num1 = scanner.nextDouble();
System.out.println("请输入你的第2个数:");
double num2 = scanner.nextDouble();
scanner.close();
System.out.println("请选择你要进行的运算(加、减、乘、除):");
Scanner scanner1 = new Scanner(System.in);
String str = " ";
str = scanner1.nextLine();
scanner1.close();
switch (str){
case "+":
sum = num1 + num2;
System.out.println("两个数的和为:" + sum);
break;
case "-":
if (num1 > num2) {
db = num1 - num2;
} else {
db = num2 - num1;
}
System.out.println("两个数的差为:" + db);
break;
case "*":
db1 = num1 * num2;
System.out.println("两个数的乘积为:" + db1);
break;
case "/":
db2 = num1 / num2;
System.out.println("两个数相除的结果是:" + db2);
break;
default:
System.out.println("你输入的运算符不对,请重新输入!");
}
}
}
}