5. 简易计算器

写一个计算器,要求实现加减乘除功能,并且能够循环接收新的数据,通过用户交互实现。

思路:

  • 写4个方法:加减乘除
  • 利用循环+switch进行用户交互
  • 传递需要操作的两个数
  • 输出结果

示例:

package com.zctou.method;

import java.util.Scanner;

public class SimpleCalculator {
    public static void main(String[] args) {
        String operator = "";
        double num1 = 0 ;
        double num2 = 0 ;
        double result = 0 ;
        Scanner scanner = new Scanner(System.in) ;

        //开场白
        System.out.println("这是一个简易的计算器");
        System.out.println("输入计算符号:+ - * /");

        //获取运算符
        operator = getOperator(scanner);
        System.out.println("你选择的运算符是:"+ operator);

        //取得num1, num2
        System.out.println("请输入第一个数字:");
        num1 = getNum(scanner);
        System.out.println("请输入第二个数字:");
        //当运算符为“/”时,分母不能为0
        do {
            num2 = getNum(scanner) ;
            if(operator.equals("/") && num2 == 0) {
                System.out.println("除法分母不能是0,请重新输入");
            }
        } while (operator.equals("/") && num2 == 0);

        //传递参数计算强结果
        result = getResult(operator,num1,num2);
        System.out.println(num1 + " " + operator + " " + num2 + "=" + result);
        scanner.close();

    }
    public static double getResult(String operator, double num1, double num2) {
        double restult = 0 ;
        switch (operator) {
            case "+":
                restult = plus(num1,num2);
                break;
            case "-":
                restult = minus(num1,num2);
                break;
            case "*":
                restult = times(num1,num2);
                break;
            case "/":
                restult = devided(num1,num2);
                break;
            default:
        }
        return  restult;
    }
    //获取数字
    public static double getNum(Scanner scanner) {
        double num = 0 ;
        boolean flag = false;
        do {
            flag = scanner.hasNextDouble();
            if(flag) {
                num = scanner.nextDouble();
                break;
            } else {
                System.out.println("非法输入,请输入数字:");
                scanner.next(); //语句作用是把上一条不合法数据删掉(相当于指针下移)
            }
        } while (!flag);

        return num ;
    }

    //根据输入的运算符选择相应的算法
    public static String getOperator (Scanner scanner){
         String operator ;
         switch(scanner.next()) {
             case "+":
                 operator = "+";
                 break;
             case "-":
                 operator = "-";
                 break;
             case "*":
                 operator = "*";
                 break;
             case "/":
                 operator = "/";
                 break;
             default:
                 System.out.println("输入的运算符有误,请重新输入");
                 operator = getOperator(scanner); //输入错误,递归调用自身
         }
        return operator ;

    }

    //加法
    public static double plus(double d1, double d2) {
        return d1+d2 ;
    }
    //减法
    public static double minus(double d1, double d2) {
        return d1-d2 ;
    }
    //乘法
    public static double times(double d1, double d2) {
        return d1*d2 ;
    }
    //除法
    public static double devided(double d1, double d2) {
        return d1/d2 ;
    }

}
Scanner.next() 方法能让 scannner 重新获取用户的输入,清除前面输入的内容,在判读数字和运算符的真假时能用上。