JAVA学到方法写了一个四则运算计算器,请教一下有什么需要改进的

JAVA学到方法写了一个四则运算计算器,请教一下有什么需要改进的

package method;
/*
 * 四则运算计算器
 * */

import java.util.Scanner;

public class Demo07
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        int a = 0;
        int num1 = 0, num2 = 0;
        System.out.println("请选择运行方法");
        System.out.println("1.加法	2.减法	3.乘法	4.除法");
        if (scanner.hasNextInt())
        {
            a = scanner.nextInt();
        }
        switch (a)
        {
            case 1 ->
            {
                System.out.println("请输入两个操作数");
                if (scanner.hasNextInt())
                {
                    num1 = scanner.nextInt();
                    num2 = scanner.nextInt();
                }
                System.out.println(add(num1, num2));
            }
            case 2 ->
            {
                System.out.println("请输入两个操作数");
                if (scanner.hasNextInt())
                {
                    num1 = scanner.nextInt();
                    num2 = scanner.nextInt();
                }
                System.out.println(sub(num1, num2));
            }
            case 3 ->
            {
                System.out.println("请输入两个操作数");
                if (scanner.hasNextInt())
                {
                    num1 = scanner.nextInt();
                    num2 = scanner.nextInt();
                }
                System.out.println(mpc(num1, num2));
            }
            case 4 ->
            {
                System.out.println("请输入两个操作数");
                if (scanner.hasNextInt())
                {
                    num1 = scanner.nextInt();
                    num2 = scanner.nextInt();
                }
                System.out.println(div(num1, num2));
            }
            default -> System.out.println("输入非法");
        }
        scanner.close();
    }

    //加法运算
    public static int add(int num1, int num2)
    {
        return num1 + num2;
    }

    //减法运算
    public static int sub(int num1, int num2)
    {
        return num1 - num2;
    }

    //乘法运算
    public static int mpc(int num1, int num2)
    {
        return num1 * num2;
    }

    //除法运算
    public static int div(int num1, int num2)
    {
        return num1 / num2;
    }
}

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » JAVA学到方法写了一个四则运算计算器,请教一下有什么需要改进的