說一個字符串包含表達式1 < 3 ...如何在if語句中評估該表達式?在if語句中評估字符串中的表達式
import java.io.*;
public class EvaluateExpression
{
public static void main(String[]args)throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
String expression;
System.out.print("Enter the expression to evaluate: ");
expression = in.readLine();
if (expression){
System.out.print("The expression " + expression + " is true.");
}else{
System.out.print("The expression " + expression + " is not true.");
}
}
}
需要一個布爾值,但找到一個字符串。有沒有一種方法來評估字符串,所以它返回一個布爾值? if (1<3) {...}
工作但if (expression) {...}
其中字符串變量表達式爲1 < 3不起作用。將其轉換爲布爾值也不起作用。
謝謝!
相似問題:http://stackoverflow.com/questions/2605032/using-eval-in-java – Allan