package com.zhou.partise;标签:Java,double,sum,System,应用,简单,println,public,out From: https://www.cnblogs.com/xzandjava/p/16585980.html
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
//写一个计算器,要求实现加减乘除功能,并且能够循环接受新的数据,通过用户交互实现。
Scanner scanner = new Scanner(System.in);
//死循环达到可以一直交互的目的
while (true){
System.out.println("=======================");
System.out.println("请输入第一个数据数据");
double a = scanner.nextDouble();
System.out.println("请输入符号(“+、-、*、/)");
String operator = scanner.next();
System.out.println("请输入第二个数据数据");
double b = scanner.nextDouble();
//通过不同的运算符号选择不同的运算操作
switch (operator){
case "+":
addition(a,b);
break;
case "-":
addition(a,b);
break;
case "*":
addition(a,b);
break;
case "/":
addition(a,b);
break;
default:
System.out.println("您输入的符号有误");
break;
}
}
}
//加法
public static void addition(double a,double b){
double sum = a+b;
System.out.println("本次计算结果是"+sum);
}
//减法
public static void subtraction(double a,double b){
double sum = a-b;
System.out.println("本次计算结果是"+sum);
}
//乘法
public static void multiply(double a,double b){
double sum = a*b;
System.out.println("本次计算结果是"+sum);
}
//除法
public static void divide(double a,double b){
double sum = a/b;
System.out.println("本次计算结果是"+sum);
}
}