当前位置:   article > 正文

Java计算器简易代码_java计算器代码

java计算器代码

B站狂神计算器作业

我写的计算器

package com.chen.method;

import java.util.Scanner;
//简易计算器
public class Demo7 {
    public static void main(String[] args) {
        //输入操作数1
        Scanner scanner1 = new Scanner(System.in);
        System.out.println("请输入操作数1:");
        float number1 = scanner1.nextFloat();

        //输入操作数2
        Scanner scanner2 = new Scanner(System.in);
        System.out.println("请输入操作数2:");
        float number2 = scanner2.nextFloat();

        //输入操作
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入操作(+,-,*,/):");
        String symbol = scanner.next();

        //调用方法
        switch (symbol){
            case "+":
               float sum = add(number1,number2);
                System.out.println(sum);
                break;
            case "-":
                float difference = subtraction(number1,number2);
                System.out.println(difference);
                break;
            case "*":
                float product = multiplication(number1,number2);
                System.out.println(product);
                break;
            case "/":
                float quotient = division(number1,number2);
                System.out.println(quotient);
                break;

        }
        scanner1.close();
        scanner2.close();
        scanner.close();


    }
    //加法
    public static float add(float a,float b){
        float sum = a + b;
        return sum;

    }
    //减法
    public static float subtraction(float c,float d){
        float difference = c - d;
        return difference;

    }
    //乘法
    public static float multiplication(float e,float f){
        float product = e * f;
        return product;

    }
    //除法
    public static float division(float g,float h){
        float quotient = g / h;
        return quotient;

    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

网上搜的进阶版本

package com.chen.method;

import java.util.Scanner;
//计算器优化
public class Demo8 {
    public static void main(String[] args) {
        System.out.println("请输入第一个数:");
        Scanner scanner = new Scanner(System.in);
        float a = scanner.nextFloat();

        System.out.println("请输入运算符(+,-,*,/):");
        Scanner scanner1 = new Scanner(System.in);
        String b = scanner1.next();

        System.out.println("请输入下一个数:");
        Scanner scanner2 = new Scanner(System.in);
        float c = scanner2.nextFloat();

        while (true) {
            //点睛之笔!
            float count = count(a,b,c);
            a = count;
            System.out.println("继续输入运算符进行运算,或输入Q退出程序...");

            b = scanner2.next();
            if (b.equals("Q")||b.equals("q")) {
                break;
            }
            System.out.println("请输入数据...");
            c = scanner1.nextFloat();

        }

    }
    public static float count(float a,String b,float c){

        switch (b) {
            case "+":
                System.out.println("当前运算结果为"+add(a, c));
                return  add(a, c);
            case "-":
                System.out.println("当前运算结果为"+sub(a, c));
                return sub(a, c);
            case "*":
                System.out.println("当前运算结果为"+mul(a, c));
                return  mul(a, c);
            case "/":
                System.out.println("当前运算结果为"+div(a, c));
                return div(a, c);
            default:
                System.out.println("非法输入");
                return 0;
        }

    };

    public static float add(float a, float b) {
        return a + b;
    }
    public static float sub(float a, float b) {
        return a - b;
    }
    public static float mul(float a, float b) {
        return a * b;
    }
    public static float div(float a, float b) {
        return a / b;
    }
}



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

拿走不谢!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号