package com.q.method;
import java.util.Scanner;
public class Calulator {
//写一个计算器,要求实现加减乘除运算,并且能够循环接收新数据,通过用户交互实现
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入需要计算的数:");
while(true) {
System.out.println("第一个数:");
double num1 = scanner.nextDouble();
System.out.println("运算符:");
String operator = scanner.next();
System.out.println("第二个数:");
double num2 = scanner.nextDouble();
switch (operator) {
case "+":
System.out.println("结果为:");
add(num1, num2);
break;
case "-":
System.out.println("结果为:");
sub(num1, num2);
break;
case "*":
System.out.println("结果为:");
product(num1, num2);
break;
case "/":
System.out.println("结果为:");
division(num1, num2);
break;
default:
System.out.println("输入不规范,同事两行泪");
}
System.out.println("请输入下一次计算所需要的数:");
}
}
//加法
public static double add(double x,double y){
double result=0;
result=x+y;
System.out.println(result);
return result;
}
//减法
public static double sub(double a,double b){
double result=0;
result=a-b;
System.out.println(result);
return result;
}
//乘法
public static double product(double x,double y){
double result=0;
result=x*y;
System.out.println(result);
return result;
}
//除法
public static double division(double a,double b){
double result=0;
result=a/b;
System.out.println(result);
return result;
}
}
原文:https://www.cnblogs.com/algm/p/14772052.html