java,在请求输入时对一个简单的数学运算进行bug

j2datikz  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(253)

我现在正在学习java,我不知道为什么我的代码会出现错误。所以,我的代码是一个简单的计算器,要求用户输入,它有4个条件。我正在测试其中一个,重要的一个,任何我想输入的东西,它都会给我答案,0.0”。条件是要求一个数学运算(作为字符串,我将其转换为char)和两个数字(例如:add 50 34)。我试了一个小时来看看问题出在哪里,我什么也找不到,我甚至搜索了我正在学习的课程,一切都是这样,但我不知道我的代码发生了什么。有人能给我解释一下吗?谢谢你。

public static void main(String[] args) {
    double[] leftVals = {100.0d, 25.0d, 225.0d, 11.0d};
    double[] rightVals = {50.0d, 92.0d, 17.0d, 3.0d};
    char[] opCode = {'d', 'a', 's', 'm'};
    double[] result = new double[opCode.length];
    if (args.length == 0) {
        for (int i = 0; i < opCode.length; i++) {
            result[i] = execute(opCode[i], leftVals[i], rightVals[i]);
        }
        for (double currentResult : result)
            System.out.println(currentResult);
    } else if (args.length == 1 && args[0].equals("interactive"))
    executeInteractively();

    else if (args.length == 3) {
        handleCommands(args);

    }else {
        System.out.println("You entered the wrong number of character. Your input needs to contain a character (a, s, n, d) followed by two numbers.");
    }
}

static void executeInteractively () {
    System.out.println("Please enter an operation and two numbers: ");
    Scanner scanner = new Scanner(System.in);
    String userInput = scanner.nextLine();
    String [] parts = userInput.split(" ");
    performOperations(parts);
}

private static void performOperations(String[] parts) {
    char opCode = opCodes(parts[0]);
    double leftVal = number(parts[1]);
    double rightVal = number(parts[2]);
    double result = execute(opCode, leftVal, rightVal);
    System.out.println(result);
}

private static void handleCommands(String[] args) {
    char opCode = args[0].charAt(0);
    double leftVal = Double.parseDouble(args[1]);
    double rightVal = Double.parseDouble(args[2]);
    double result = execute(opCode, leftVal, rightVal);
    System.out.println(result);
}

static double execute (char opCode, double value1, double value2) {
    double result;
    switch (opCode) {
        case 'a':
            result = value1 + value2;
            break;
        case 's':
            result = value1 - value2;
            break;
        case 'm':
            result = value1 * value2;
            break;
        case 'd':
            result = value2 != 0 ? value1 / value2 : 0.0D;
            break;
        default:
            System.out.println("Invalid opCode: " + opCode);
            result = 0.0d;
            break;
    }
    return result;
}
static char opCodes (String name) {
    char opCode = name.charAt(0);
    return opCode;
}
static double number (String theNumber) {
    String[] numberOfWords = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    double value = 0d;
    for (int index = 0; index<numberOfWords.length; index++ ){
        if (theNumber.equals(numberOfWords[index])) {
            value = index;
            break;
        }
    }
    return value;
}
tmb3ates

tmb3ates1#

您已经定义了方法 number() 它接受一个字符串,并返回一个double。它支持的字符串类型类似于“1”、“2”等,而不是“1”、“2”。。。
你说过你试过输入 add 50 34 ,根据您的代码,它将始终返回0.0
尝试提供输入 add five three 看看你能得到什么结果。
当前代码不支持输入大于9的计算。而且用户必须用文字提供值,所以他们不能键入9,他们必须键入'9'这个词。

相关问题