我有三个变量 int value1、value2 和一个 String 运算符。我想检查 if 条件。假设 value1=4,value2=3 并且在运算符='>'中。那么我如何检查示例的 if 条件 -
int value1=4;
int value2=3;
String operator=">";
if (value1+operator+value2)
{
System.out.println("hello");
}
i know the this is wrong but how to correct it..
最佳答案
您将无法直接使用字符串。您必须首先检查它是什么类型的运算符,然后执行显式数学运算。
public boolean applyOperator(String op, int a, int b) {
if (">".equals(op)) {
return a > b;
} // the others are an exercise to the reader.
}
// later in the code
if(applyOperator(operator, value1, value2)) {
System.out.println("Hello world!");
}
关于java - if 条件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15562793/