java - 基于用户输入的结果

标签 java methods arguments calculator

我一直在学习枚举和 switch 语句,并且我有以下代码

我不想通过代码运行参数,然后编译它以查看结果,我希望用户将数字输入终端和 +,x,/,- 并在终端中返回结果,我该如何完成这个

public class MathCalculatorTest2_0 {
MathFunctions1_0 mathFunction1;
public MathCalculatorTest2_0 (MathFunctions1_0 mathFunction1) {
    this.mathFunction1 = mathFunction1;
}

public void DoMath(double digit1, double digit2) {
    double mathResult = 0;
    switch (mathFunction1) {
        case MULTIPLY:
            mathResult = digit1*digit2;
            break;  
        case DIVIDE:
            mathResult = digit1/digit2;
            break;
        case ADD:
            mathResult = digit1+digit2;
            break;
        case SUBTRACT:
            mathResult = digit1-digit2;
            break;
        default:
            System.out.println("Incorrect arithmatic character, please insert a correct arithmatic character between the two numbers (x, /, +, -)");
    }
    System.out.println(mathResult);
}

public static void main(String[] args) {
    if (args.length < 3) {
        System.out.println("Please correct syntax");
    } else {
        double firstDigit = Double.parseDouble(args[0]);
        String userInput1 = args[1];
        double secondDigit = Double.parseDouble(args[2]);
        MathCalculatorTest2_0 C;
        switch (userInput1) {
            case "x":
                C = new MathCalculatorTest2_0(MathFunctions1_0.MULTIPLY);
                C.DoMath(firstDigit, secondDigit);
                break;
            case "/":
                C = new MathCalculatorTest2_0(MathFunctions1_0.DIVIDE);
                C.DoMath(firstDigit, secondDigit);
                break;
            case "+":
                C = new MathCalculatorTest2_0(MathFunctions1_0.ADD);
                C.DoMath(firstDigit, secondDigit);
                break;
            case "-":
                C = new MathCalculatorTest2_0(MathFunctions1_0.SUBTRACT);
                C.DoMath(firstDigit, secondDigit);
                break;
            default: System.out.println("Please insert correct arithmatic character.");
        }
    }
}

}

public enum MathFunctions1_0 {
MULTIPLY, DIVIDE, ADD, SUBTRACT

}

最佳答案

首先,你的代码中有很多不好的东西,这可能不是它应该看起来的最好版本,但无论如何它要好得多。使用 switch 运算符会很好,并且可以为需要执行的操作声明一些静态最终变量。 声明其中包含一些数学工具的静态方法和字段的类:

public class MathTool {
    public static final int PLUS_OPERATION = 0;

    public static final int MINUS_OPERATION = 1;

    public static final int MULTIPLY_OPERATION = 2;

    public static final int DIVIDE_OPERATION = 3;

    public static double doMathOperation(double digit1, double digit2, int operation) {
        switch (operation) {
        case PLUS_OPERATION: return digit1 + digit2;
        case MINUS_OPERATION: return digit1 - digit2;
        case MULTIPLY_OPERATION: return digit1 * digit2;
        case DIVIDE_OPERATION: return digit1 / digit2;
        default: return 0;
        }
    }
}

然后你可以像这样调用它的方法:

    public static void main(String[] args) {
        double operationResult = MathTool.doMathOperation(5, 3, MathTool.MULTIPLY_OPERATION);
        System.out.println(operationResult);
    }

在 Java 中,变量名的首字母应该使用小写。

编辑:

对于来自控制台的用户输入,请使用扫描仪,如下所示:

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

关于java - 基于用户输入的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19939587/

相关文章:

java - 重新创建 Activity 时保留变量值

java - 当代码不是项目时将代码导入到 Eclipse 中

java - 如何获取 DOM 对象中两个元素之间的文本?

c++ - Const 方法 - 在实现中重复?

java - 为什么我们不能用私有(private)扩展类方法覆盖基类方法?

java - 为什么我的 block 的颜色不输出

java - (Libgdx) 使舞台和 spritebatch 中的一切变暗

linux - 如何从 HH :MM Format for a Shell Script 中的参数中捕获值

Java WS 应用程序偶尔忽略参数

ios - 从 Swift 类在 Objective-C 类的类方法 (+) 中传递参数