java代码写的简易计算器(可以实现基本的加减乘除功能)

计算器

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

import java.util.Scanner;

/**
 * 计算器
 * 写4个方法:加减乘除
 * 利用循环加switch进行用户交互
 * 传递需要操作的两个数
 * 输出结果
 */
public class Demo04 {
    public static double a;
    public static double b;
    //运算符
    public static String operator;
    //=符
    public static String equal;
    //结果
    public static double sum=0;
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.println("输入数字:");
        a = scanner.nextDouble();
        System.out.println("输入运算符:");
        operator = scanner.next();
        System.out.println("输入数字:");
        b = scanner.nextDouble();
        do {
            switch (operator){
                case "+":
                    sum= add(a, b);
                    System.out.println("输入=获取结果:");
                    equal = scanner.next();
                    coninue();
                    break;
                case "-":
                    sum=subtract(a,b);
                    System.out.println("输入=获取结果:");
                    equal = scanner.next();
                    coninue();
                    break;
                case "*":
                    sum=multiply(a,b);
                    System.out.println("输入=获取结果:");
                    equal = scanner.next();
                    coninue();
                    break;
                case "/":
                    if (b==0){
                        System.out.println("被除数不能为0");
                        operator = "q";
                        break;
                    }else {
                        sum=divide(a,b);
                        System.out.println("输入=获取结果:");
                        equal = scanner.next();
                        coninue();
                        break;
                    }
                default:
                    System.out.println("运算符输入错误!");
            }
        }while (operator.equals("+")||operator.equals("-")||operator.equals("*")||operator.equals("/"));
        scanner.close();
    }

    //加
    public static double add(double a,double b){
        return a+b;
    }
    //减
    public static double subtract(double a,double b){
        return a-b;
    }
    //乘
    public static double multiply(double a,double b){
        return a*b;
    }
    //除
    public static double divide(double a,double b){
        return a/b;
    }
    //获得结果 或 再次输入
    public static void coninue(){
        Scanner scanner = new Scanner(System.in);
        if (equal.equals("=")){
            System.out.println(a+operator+b+"="+sum);
            System.out.println("输入运算符+,-,*,/继续 或 输入其他字符结束");
            operator=scanner.next();
            if (operator.equals("+")||operator.equals("-")||operator.equals("*")||operator.equals("/")){
                System.out.println("输入数字:");
                b = scanner.nextDouble();
                a=sum;
            }else {
                System.out.println("输入错误!");
            }
        }else {
            System.out.println("输入错误!");
        }
    }
}

你可能感兴趣的:(代码,java,开发语言,后端)