计算器实现加减乘除

写一个计算器

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

思路:

1.写4个方法:加减乘除

2.利用循环+switch进行用户交互

3.传递需要操作的两个数

4.输出结果

import java.util.Scanner;

public class computer {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        computer computer = new computer();
        int intNum1 = 0;
        int intNum2 = 0;
        float floatNum1 = 0.0f;
        float floatNum2 = 0.0f;
        String num1 = "";
        String num2 = "";
        String sign = "请检查运算符号输入是否正确!";
        System.out.println("请输入第一个数:");
        if(scanner.hasNextInt()){
            intNum1 = scanner.nextInt();
            num1 = String.valueOf(intNum1);
        }else if(scanner.hasNextFloat()){
            floatNum1 = scanner.nextFloat();
            num1 = String.valueOf(floatNum1);
        }
        System.out.println("请输入 + 或 - 或 * 或 / 任意一个运算符号:");
        if(scanner.hasNext()){
            sign = scanner.next();
        }
        System.out.println("请输入第二个数:");
        if(scanner.hasNextInt()){
            intNum2 = scanner.nextInt();
            num2 = String.valueOf(intNum2);
        }else if(scanner.hasNextFloat()){
            floatNum2 = scanner.nextFloat();
            num2 = String.valueOf(floatNum2);
        }
        int result = 0;
        if(judgeIsDecimal(num1) == false) {
            switch (sign)
            {
                case "+":
                    result = computer.add(Integer.parseInt(num1), Integer.parseInt(num2));
                    break;
                case "-":
                    result = computer.sub(Integer.parseInt(num1), Integer.parseInt(num2));
                    break;
                case "*":
                    result = computer.mul(Integer.parseInt(num1), Integer.parseInt(num2));
                    break;
                case "/":
                    result = computer.div(Integer.parseInt(num1), Integer.parseInt(num2));
                    break;
                default:
                    System.out.println("请检查运算符号输入是否正确!");
            }
        }else if(judgeIsDecimal(num1) == true){
                switch (sign)
                {
                    case "+":
                        result = computer.add(Integer.parseInt(num1), Integer.parseInt(num2));
                        break;
                    case "-":
                        result = computer.sub(Integer.parseInt(num1), Integer.parseInt(num2));
                        break;
                    case "*":
                        result = computer.mul(Integer.parseInt(num1), Integer.parseInt(num2));
                        break;
                    case "/":
                        result = computer.div(Integer.parseInt(num1), Integer.parseInt(num2));
                        break;
                    default:
                        System.out.println("请检查运算符号输入是否正确!");
                }
        }else {
            System.out.println("请检查输入是否为整数或小数!");
        }
        System.out.println(intNum1+" "+sign+" "+intNum2+" = "+result);
        scanner.close();
    }

    public static boolean judgeIsDecimal(String num){

        boolean isdecimal = false;

        if (num.contains(".")) {

            isdecimal=true;

        }

        return isdecimal;

    }
    public static int add(int a,int b){
        int result = a + b;
        return result;
    }
    public static float add(float a,float b){
        float result = a + b;
        return result;
    }

    public static int sub(int a,int b){
        int result = a - b;
        return result;
    }
    public static float sub(float a,float b){
        float result = a - b;
        return result;
    }

    public static int mul(int a,int b){
        int result = a * b;
        return result;
    }
    public static float mul(float a,float b){
        float result = a * b;
        return result;
    }
    public static int div(int a,int b){
        int result = a / b;
        return result;
    }
    public static float div(float a,float b){
        float result = a / b;
        return result;
    }
}
posted @ 2023-09-18 13:18  chengh0618  阅读(118)  评论(0编辑  收藏  举报