2014-10-10 79 views
0

我在下面的代碼的if語句中遇到問題。 即使條件正確,我也無法登錄if語句。我的語法正確嗎?Selenium的Java - 字符串比較

String NicotineProduct="Not in the last 5 years" 

if (NicotineProduct=="No, Never"||NicotineProduct=="Not in the last 5 years"||NicotineProduct=="Not in the last 3 years") 
{  
    if (rateclass=="Pref Best No Nicotine") 
      rateclassval=1; 
    else if (rateclass=="Pref No Nicotine") 
      rateclassval=2; 
    else if (rateclass=="Select No Nicotine") 
      rateclassval=3; 
    else if (rateclass=="Standard No Nicotine") 
      rateclassval=4; 
} 
+0

不,你的語法不正確,因爲它沒有顯示你想要的語義。對於字符串比較,始終使用[String.equals()](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#equals%28java.lang.Object%29);一切都意味着要求麻煩。 – JimmyB 2014-10-10 13:59:09

回答

0

始終使用.equalsString比較:

if (NicotineProduct.equals("No, Never")||NicotineProduct.equals("Not in the last 5 years")||NicotineProduct.equals("Not in the last 3 years")) { 
    if (rateclass.equals("Pref Best No Nicotine")) 
     rateclassval=1; 
    else if (rateclass.equals("Pref No Nicotine")) 
     rateclassval=2; 
    else if (rateclass.equals("Select No Nicotine")) 
     rateclassval=3; 
    else if (rateclass.equals("Standard No Nicotine")) 
     rateclassval=4; 
} 

看這個其它的問題/答案:

How do I compare strings in Java?

0

String.equals()是用於比較字符串值的優選方式。