2014-10-10 83 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()是用于比较字符串值的优选方式。