[英]Evaluate user expression
我正在開發一個有文本框的Web應用程序。 用戶輸入條件以顯示“字符串”的特定顏色。 該字符串包含一些整數值。
用戶像這樣指定字符串值的條件,
>30:"Red"
<20:"Green"
:"Yellow:
這是一個條件。 這就像if,elseif,else條件。 用戶只有在這樣的情況下才能指定
>60:"Blue"
:"White"
(這意味着如果字符串值大於60,則將“藍色”作為字符串的顏色,否則將“白色”作為字符串的顏色)
我必須根據在文本框中為每個字符串輸入的上述條件來評估字符串的顏色。 有成千上萬個帶有值的字符串,用戶將為每個字符串輸入這樣的表達式。
解決此問題的最佳方法是什么?
String input = getTextBoxInput() // get input from text box
if(input.contains("<") || input.contains("<"))
{
if(input.contains("<"))
{
int index = input.indexOf("<");
String conditionString = input.substring(index+1,input.length());
// condition number is the number being compared
int conditionNumber = conditionString.split(":")[0];
// if condition is true then get the corresponding colour
if(number < conditionNumber){setColor(conditionString.split(":")[1])}
// check if it an if else condition and do the following
else if(input.length()>1){ setColor(conditionString.split(":")[2])}
}
}
這是基本的偽代碼。 您可以為“ <”和“>”定義常量。 另外,在輸入錯誤的情況下,您可能需要執行一些異常處理。
請參閱Shunting Yard算法,您需要具有類似的內容
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.