将简单计算器更改为算术表达式

时间:2015-09-17 09:47:52

标签: java

我对如何将其改为算术表达式一无所知。

它应该做同样的事情,但不是使用来自用户的3个输入,它应该是1输入。

readDouble();readChar();是从包中导入的预制扫描程序方法。需要帮助。

import static dit948.SimpleIO.*;

public class Exce1 {
public static void main(String[] args) {
    double n1, n2;
    char operation;



    println("Enter the first number: ");
    n1 = readDouble();

    if(n1 == 0){
        println("The program is terminated. Bye");
        System.exit(0);
    }


    println("Enter the operator: ");
    operation = readChar();

    println("Enter the second number: ");
    n2 = readDouble();

    switch(operation){
    case '+':
    println("The result is " + (n1 + n2));
    break;
    case '-':
        println("The result is " + (n1 - n2));
        break;
    case '*': 
        println("The result is " + (n1 * n2));
        break;
    case '/':
        println("The result is " + (n1 / n2));
        break;
    }
        }
}

2 个答案:

答案 0 :(得分:1)

使用ScriptEngineManager& ScriptEngine执行表达式的评估

    import java.util.Scanner;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptEngine;
    import javax.script.ScriptException;

    public class myCalc {
        public static void main(String args[]){


                Scanner input = new Scanner(System.in);

                String x="";
                boolean done = false;


                    while (done==false){
                    System.out.print("Please enter your operation: ");

                    x = input.nextLine();


                    ScriptEngineManager mgr = new ScriptEngineManager();
                    ScriptEngine engine = mgr.getEngineByName("JavaScript");

                    try {
                        System.out.println(engine.eval(x));
                    } catch (ScriptException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    }

                       input.close();

        }
    }

此外,有人评论说OP是一个学习者(虽然尝试最佳解决方案从不会伤害但这并不重要),这是另一个解决方案 -

import java.util.Scanner;


public class apples {
    public static void main(String args[]){


            Scanner input = new Scanner(System.in);
            double answer = 0;
            double inputA, inputB;
            char operator;
            String x="";
            String[] oper;
            String[] nos;
            boolean done = false;


                while (done==false){
                System.out.print("Please enter your operation: ");

                x = input.nextLine();
                oper = x.split("[^*/+-]");
                nos = x.split("[^0-9]");


                inputA = Double.parseDouble(nos[0]);
                inputB = Double.parseDouble(nos[nos.length-1]);
                operator = oper[oper.length-1].charAt(0);



               switch (operator) {
                    case '+': answer = inputA + inputB;
                              break;
                    case '-': answer = inputA - inputB;
                              break;
                    case '*': answer = inputA * inputB;
                              break;
                    case '/': answer = inputA / inputB;
                              break;

                }

                   System.out.println(answer);   
                }
                   input.close();

    }
}

答案 1 :(得分:0)

因为看起来你只是一个学习者,所以你必须经历一些示例代码:

  public static void main(String[] args) {
    // TODO code application logic here
    String exp=null;
    Scanner scan=new Scanner(System.in);

    System.out.println("Please enter an Arithmetic Expression! (2*2)");
    exp=scan.nextLine(); //read the whole line

    //check if the string contains +, -, / or * and then split and perform the required function
    if (exp.contains("+")) {
        String[] array=exp.split("\\+");
        int n1=Integer.parseInt(array[0]);
        int n2=Integer.parseInt(array[1]);
        int sum=n1+n2;
        System.out.println("Answer: "+sum);
    }

    if (exp.contains("-")) {
        String[] array=exp.split("\\-");
        int n1=Integer.parseInt(array[0]);
        int n2=Integer.parseInt(array[1]);
        int sub=n1-n2;
        System.out.println("Answer: "+sub);
    }

    if (exp.contains("/")) {
        String[] array=exp.split("\\/");
        int n1=Integer.parseInt(array[0]);
        int n2=Integer.parseInt(array[1]);
        int div=n1/n2;
        System.out.println("Answer: "+div);
    }

    if (exp.contains("*")) {
        String[] array=exp.split("\\*");
        int n1=Integer.parseInt(array[0]);
        int n2=Integer.parseInt(array[1]);
        int mul=n1*n2;
        System.out.println("Answer: "+mul);
    }
}