2014-09-11 54 views
0

以下是一段代码将字符串与输入表单用户进行比较。 java的

System.out.println("How would you describe your lifestyle? Sedentary, Somewhat Active, Active, Highly Active?"); 
    String lifestyle = keyboard.next(); 
if (lifestyle.equalsIgnoreCase("Somewhat Active")) 
{ 
    System.out.println("ok"); 
} 
else 
{ 
    System.out.println("not ok") 
} 

的不管是什么我型我不能让一个“OK”的回应。

回答

3

Scanner#next()将始终返回下一个令牌,这将只是“有点”。改为使用keyboard.nextLine()

+0

但如何“有点”.equalsIgnoreCase(“有点活跃”)它会返回true? – 2014-09-11 00:40:54

+1

@JunedAhsan不,这两个字符串不相等。然而,“someWHAT eQuAL”在这样的比较中是可以接受的。 OP目前使用'next()',它只返回“有点”,这当然不会按照预期进行比较。 – hexafraction 2014-09-11 00:41:41

0

您应该尝试使用hexafraction建议的nextLine()

String lifestyle = keyboard.next(); //<-- input without space, Somewhat 
String lifestyle = keyboard.nextLine(); //<-- input with spaces, Somewhat Active 
相关问题