如何用Java计算结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19278497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate the result in Java?
提问by MOTIVECODEX
I created this code to create random numbers and operators, but how can it calculate and show the result?
我创建了这段代码来创建随机数和运算符,但它如何计算和显示结果?
What it does now is for example print out: 4+1-3 or 9*2-8 etc. I don't know how to calculate the result of 4+1-3 or 9*2-8 and print it out.
它现在所做的是例如打印出: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++;
}
}
}
采纳答案by Tim S.
This is some (relatively) simple code that will calculatethe expression left-to-right (it doesn't take into account the order of operations, so 3+4*5
is evaluated as (3+4)*5
, not the correct 3+(4*5)
):
这是一些(相对)简单的代码,它将计算从左到右的表达式(它不考虑操作顺序,因此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();
}
}
回答by angel_navarro
That is not a simple questions because you must keep in mind the priority of arithmetical operators, so my guess is you must use a mathematical library that helps you. For example, Formula4j: http://www.formula4j.com/index.html
这不是一个简单的问题,因为您必须牢记算术运算符的优先级,所以我的猜测是您必须使用一个可以帮助您的数学库。例如,Formula4j:http: //www.formula4j.com/index.html
回答by 9000
Are you stuck applying operations to arguments?
您是否坚持将操作应用于参数?
The simplest way:
最简单的方法:
if ("+".equals(operator)) {
result = arg1 + arg2;
} else if ...
System.out.println("See, " + arg1 + operator + arg2 " = " + result);
A slightly more extensible way that uses a hash table instead of an endless if
:
使用哈希表而不是无穷无尽的稍微更可扩展的方式if
:
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);
...
}
If you think this is needlessly long, it is. Still something like this is the typical pattern in Java-land. (That's why Scala exists, but it's another story.)
如果你认为这是不必要的长,那就是。这仍然是 Java 领域的典型模式。(这就是 Scala 存在的原因,但这是另一回事。)
This does not even touch operation priority or parentheses.
这甚至不涉及操作优先级或括号。