初学:Java语言编写一个计算器

package JavaLearn.Method;

import java.util.Scanner;

public class HomeWork {
    public static void main(String[] args) {
        int a = 0;
        int b = 0;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入运算符:");
        String operator = scanner.nextLine();
        System.out.println("请输入第一个运算数:");
        a = scanner.nextInt();
        System.out.println("请输入第二个运算数:");
        b = scanner.nextInt();
        scanner.close();
        System.out.println("===============");
        switch (operator){
            case "+":
                System.out.println(a+operator+b+"="+add(a,b));
                break;
            case "-":
                System.out.println(a+operator+b+"="+sub(a,b));
                break;
            case "*":
                System.out.println(a+operator+b+"="+mul(a,b));
                break;
            case "/":
                System.out.println(a+operator+b+"="+div(a,b));
                break;
        }
    }
    public static double add(int a,int b){
        return a+b;
    }
    public static double sub(int a,int b){
        return a-b;
    }
    public static double mul(int a,int b){
        return a*b;
    }
    public static double div(int a,int b){
        return a/b;
    }
}

上一篇:Operator 示例:使用 Redis 部署 PHP 留言板应用程序


下一篇:C++ //谓词 //一元谓词 //概念:返回bool类型的仿函数称为 谓词 //如果 operator()接受一个参数,那么叫做一元谓词 //如果 operator()接受 2 个参数,那么