简易计算器

简易计算器,第1张

简易计算器
import java.util.Scanner;

public class Calc {
    public double add(double a,double b){
        return a+b;
    }
    public double red(double a,double b){
        return a-b;
    }
    public double ride(double a,double b){
        return a*b;
    }
    public double except(double a,double b){
        return a/b;
    }
    public static void main(String[] args) {
        Calc calc = new Calc();
        while(true){
            System.out.println("请输入+ - * /或quit退去计算器");
            Scanner scanner = new Scanner(System.in);
            String s = scanner.nextLine();
            if(s.equals("quit")){
                System.out.println("感谢使用");
                break;
            }
            System.out.println("请输入两个数");
            Scanner scan1 = new Scanner(System.in);
            double a = scan1.nextDouble();
            Scanner scan2 = new Scanner(System.in);
            double b = scan2.nextDouble();
            switch (s){
                case "+":
                    System.out.println(a+s+b+"="+calc.add(a,b));
                    break;
                case "-":
                    System.out.println(a+s+b+"="+calc.red(a,b));
                    break;
                case "*":
                    System.out.println(a+s+b+"="+calc.ride(a,b));
                    break;
                case "/":
                    System.out.println(a+s+b+"="+calc.except(a,b));
                    break;
                default:
                    System.out.println("请重新输入");
                    break;
            }
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5672739.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存