Java 简单的计算器操作

Java 简单的计算器操作,java,math,calculator,Java,Math,Calculator,我试图简化我的长代码的计算器程序,但我有一个路障。对于每个计算器操作符,我都有一个新的else-if语句,但我想做的是允许用户在一行上手动输入他们想要执行的整个操作,并让代码计算它 以下是我所拥有的: do { System.out.println("What function would you like to perform?"); System.out.print("Exit Calculator (Q), Add (+), Subtract (-), Mu

我试图简化我的长代码的计算器程序,但我有一个路障。对于每个计算器操作符,我都有一个新的else-if语句,但我想做的是允许用户在一行上手动输入他们想要执行的整个操作,并让代码计算它

以下是我所拥有的:

do {
        System.out.println("What function would you like to perform?");
        System.out.print("Exit Calculator (Q), Add (+), Subtract (-), Multiply (x), Divide (/): ");
        maininput = in.next();

        if (maininput.equals("+")) {
            System.out.print("Enter the first number to add: ");
            num1 = in.nextDouble();
            System.out.print("Enter the second number to add: ");
            num2 = in.nextDouble();
            System.out.println();

            answer = num1 + num2;

            System.out.println(num1 + " + " + num2 + " = " + answer);
            System.out.println();
        }
        else if (maininput.equals("-")) {
            System.out.print("Enter the first number to subtract: ");
            num1 = in.nextDouble();
            System.out.print("Enter the second number to subtract: ");
            num2 = in.nextDouble();
            System.out.println();

            answer = num1 - num2;

            System.out.println(num1 + " - " + num2 + " = " + answer);
            System.out.println();
        }
        else if(maininput.equals("x")) {
            System.out.print("Enter the first number to multiply: ");
            num1 = in.nextDouble();
            System.out.print("Enter the second number to multiply: ");
            num2 = in.nextDouble();
            System.out.println();

            answer = num1 * num2;

            System.out.println(num1 + " x " + num2 + " = " + answer);
            System.out.println();
        }
        else if(maininput.equals("/")) {
            System.out.print("Enter the first number to divide: ");
            num1 = in.nextDouble();
            do {
                System.out.print("Enter the second number to divide: ");
                num2 = in.nextDouble();
                System.out.println();
                if (num2 == 0) {
                    System.out.println("Cannot divide by 0!  Please enter a different number.");
                }
            } while (num2 == 0);

            answer = num1 / num2;

            System.out.println(num1 + " / " + num2 + " = " + answer);
            System.out.println();
        }
        else if(maininput.equals("Q") || maininput.equals("q") || maininput.equals("EXIT") || maininput.equals("exit")) {
            in.close();
            System.exit(0);
        }
        else {
            System.out.println(maininput + " is not a valid operand.  Please try again.");
            System.out.println();
        }
    } while (maininput != "Q" && maininput != "q");
这就是我想要的输出:

Enter operation:
4 * 6
4 * 6 = 24

应该能够在一行上输入任何操作。我不是要你帮我写计算器,我是在问如何让计算机从一行上读取整个操作,然后计算,然后打印出来。

如果你使用扫描仪readLine,那么你可以读取整行

e、 g

然后可以拆分此行以获得三个令牌

 String tokens [] = line.split (" ");
然后您可以看到基于令牌[1]要执行的操作

 if (token[1].equals ("-") {

      //lets minus token[2] from token[0]
      // need to convert String to Number
 }

可以使用String.split并将其存储在数组中。然后它将返回一个字符串数组,将这些字符串解析回整数。用户可以执行您想要的操作。x变量将是结果

    if(maininput.contains("+")) {
        String[] stringarr = string.split("\\+");
        int x = Integer.parseInt(stringarr[0])  +  Integer.parseInt(stringarr[1]);
        System.out.println(stringarr[0] + " + " + stringarr[1] + " = " + x);
    } else if(maininput.contains("-")) {
        String[] stringarr = string.split("\\-");
        int x = Integer.parseInt(stringarr[0])  -  Integer.parseInt(stringarr[1]);
        System.out.println(stringarr[0] + " - " + stringarr[1] + " = " x);
    } 

。。。依此类推。

您可以尝试使用模式对象解析该行,如下所示:

Pattern opPattern = Pattern.compile("(\\d+) *([+-*/]) *(\\d+)");
Matcher matcher = opPattern.matcher(userLine);
if(matcher.find()) {
    int op1 = Integer.toValue(matcher.group(1));
    int op2 = Integer.toValue(matcher.group(3));
    String op = matcher.group(2);

    if(op.equals("+")) {
        // do + op ...
    } else ... {
        // etc...
    }

} else {
    // error in line, not the form of an operation
}
看看javadoc,因为我不确定我是否使用了正确的方法名等等,只是试图说明这个想法

Pattern opPattern = Pattern.compile("(\\d+) *([+-*/]) *(\\d+)");
Matcher matcher = opPattern.matcher(userLine);
if(matcher.find()) {
    int op1 = Integer.toValue(matcher.group(1));
    int op2 = Integer.toValue(matcher.group(3));
    String op = matcher.group(2);

    if(op.equals("+")) {
        // do + op ...
    } else ... {
        // etc...
    }

} else {
    // error in line, not the form of an operation
}