【发布时间】:2015-09-09 18:13:04
【问题描述】:
如何在 if/then 语句中比较一个字符串和多个单词?我试过这个:
System.out.println("How would you describe your active lifestyle? Sedentary, Somewhat Active, or Active?");
String lifestyle = sc.next();
double activity;
if(lifestyle.equalsIgnoreCase("sedentary"))
{
activity = 0.2;
}
else if(lifestyle.equalsIgnoreCase("somewhat active"))
{
activity = 0.3;
}
else
{
activity = 0.5;
}
但是当您输入“有点活跃”时,它会将变量活动分配为 0.5。我怎样才能让它注册“有点活跃”的东西?
【问题讨论】:
-
另外,您应该寻找 Somewhat Active 或 Sedentary,而不是 Sedentary 或有点活跃。 (案例)更改后 sc.nextLine().
标签: java string if-statement comparison