2013-10-09 84 views
2

我创建了这段代码来创建随机数字和运算符,但它如何计算并显示结果呢?如何计算Java中的结果?

它现在的功能是例如打印输出:4 + 1-3或9 * 2-8等 我不知道如何计算4 + 1-3或9 * 2-8的结果并打印出来。

public static void main(String[] args) { 
    int t = 0; 
    String[] operators = {"-", "+", "*"}; 
    String operator; 
    Random r = new Random(); 

    for (int i = 0; i < 3; i++) { 
     int randNum = randNums(9, 1); 
     operator = operators[r.nextInt(operators.length)]; 
     System.out.print(randNum); 
     if (t < 2) { 
      System.out.print(operator); 
      t++; 
     } 
    } 

} 
+0

应该以正确的方式评估3 + 4 * 5吗? –

+0

@Tim S,暂时还没有,这将是我的下一步,但首先我想解决这个问题,看看我是否可以继续 – MOTIVECODEX

+0

@davecom,它不是重复的,正如ciemborg在这篇文章中所说的:“为此,运行JS引擎“非常”在顶端“。我已经有了那部分,它随机化了一切。我只需要计算它,所以它不是重复的。 – MOTIVECODEX

回答

1

这是一些(相对)简单代码will calculate左到右的表达式(它并没有考虑到操作的顺序,所以3+4*5被评价为(3+4)*5,不正确的3+(4*5)):

public static void main(String[] args) { 
    String[] operators = {"-", "+", "*"}; 
    String operator; 
    Random r = new Random(); 
    int result = -1; 

    for (int i = 0; i < 3; i++) { 
     int randNum = r.nextInt(9) + 1; // 1 - 9 inclusive 
     if (i != 0) { 
      operator = operators[r.nextInt(operators.length)]; 
      System.out.print(operator); 
      result = calculate(result, randNum, operator); 
     } 
     else { 
      result = randNum; 
     } 
     System.out.print(randNum); 
    } 
    System.out.println("=" + result); 
} 
public static int calculate(int operand1, int operand2, String operator) { 
    switch (operator) { 
     case "+": 
      return operand1 + operand2; 
     case "-": 
      return operand1 - operand2; 
     case "*": 
      return operand1 * operand2; 
     default: 
      throw new RuntimeException(); 
    } 
} 
+0

感谢您按照我的意愿工作+ – MOTIVECODEX

3

这不是一个简单的问题,因为你必须牢记算术运算符的优先级,所以我的猜测是,你必须使用一个数学库,可以帮助你。例如,Formula4j: http://www.formula4j.com/index.html

+0

我已经有了可以用于正确优先级的那部分,我只需要像现在这样计算它 – MOTIVECODEX

+0

如果您看到此库的“示例”部分,您将很容易使用,它可以计算直接从包含您的公式的字符串中得到的结果 –

1

您是否坚持将操作应用于参数?使用哈希表,而不是无尽的if

if ("+".equals(operator)) { 
    result = arg1 + arg2; 
} else if ... 
System.out.println("See, " + arg1 + operator + arg2 " = " + result); 

一个稍微扩展的方式:

最简单的方法

import java.util.*; 

// Poor man's first-class function 
interface BinaryOp { 
    int apply(int arg1, int arg2); 
} 

final static BinaryOp ADD = new BinaryOp() { 
    public int apply(int arg1, int arg2) { return arg1 + arg2; } 
} 

final static BinaryOp SUBTRACT = new BinaryOp() { 
    public int apply(int arg1, int arg2) { return arg1 - arg2; } 
} 

final static BinaryOp MULTIPLY = new BinaryOp() { 
    public int apply(int arg1, int arg2) { return arg1 * arg2; } 
} 

static final Map<String, BinaryOp> OPERATIONS = new HashMap<String, BinaryOp>(); 

// This replaces the 'if', easier to extend. 
static { 
    OPERATIONS.put("+", ADD); 
    OPERATIONS.put("-", SUBTRACT); 
    OPERATIONS.put("*", MULTIPLY); 
} 

public static void main(String[] args) { 
    ... 
    BinaryOp operation = OPERATIONS.get(operation_name); 
    int result = operation.apply(arg1, arg2); 
    ... 
} 

如果你认为这是不必要的长,它是。这样的东西仍然是Java-land中的典型模式。 (这就是为什么Scala存在,但它是另一回事。)

这甚至不触及操作优先级或括号。