编程语言
首页 > 编程语言> > Ex2使用Java语言实现简单的加减乘除计算器

Ex2使用Java语言实现简单的加减乘除计算器

作者:互联网

使用Java语言实现简单的加减乘除计算器

import java.util.Scanner;

import static java.lang.System.exit;

public class Calculate {
    static void show()
    {
        System.out.println("请输入你要选择的功能(输入数字)");
        System.out.println("1.加法");
        System.out.println("2.减法");
        System.out.println("3.乘法");
        System.out.println("4.除法");
        System.out.println("0.退出");
    }
    static int add(int a,int b)
    {
        return a+b;
    }
    static int re(int a,int b)
    {
        return a-b;
    }
    static int mul(int a,int b)
    {
        return a*b;
    }
    static int di(int a,int b)
    {
        return a/b;
    }
    public static void main(String[] args)
    {
        show();
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        while (choice!=0) {
            System.out.println("请输入你要计算的两个数字");
            int num1 = scanner.nextInt();
            int num2 = scanner.nextInt();
            System.out.println("result:");
            switch (choice) {
                case 1:
                    System.out.println(add(num1, num2));
                    break;
                case 2:
                    System.out.println(re(num1, num2));
                    break;
                case 3:
                    System.out.println(mul(num1, num2));
                    break;
                case 4:
                    System.out.println(di(num1, num2));
                    break;
                default:
                    System.out.println("没有此功能,请重新选择功能");
            }
            show();
            choice = scanner.nextInt();
        }
    }
}

标签:Java,num2,int,System,Ex2,println,static,加减乘除,out
来源: https://blog.csdn.net/timoraty/article/details/112133032