Java for Selenium-字符串比较



我在下面代码的if语句中遇到了一个问题。即使条件正确,我也无法登录if语句。我的语法正确吗?

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;
 }

始终使用.equals进行String比较:

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;
}

请参阅其他问题/答案:

如何在Java中比较字符串?

String.equals()是比较字符串值的首选方法。

最新更新