将字符串与输入表单用户进行比较。JAVA

将字符串与输入表单用户进行比较。JAVA,java,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");

下面是一段代码

    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”响应。

将始终返回下一个标记,它将只是“稍微”。请改用。

您应该按照hexafraction的建议尝试
nextLine()

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

String生活方式=键盘.next()//但它如何“稍微”。equalsIgnoreCase(“稍微活跃”)返回true?@JunedAhsan不,这两个字符串不相等。然而,在这样的比较中,“稍微相等”是可以接受的。OP当前使用的是
next()
,它只返回“稍微”的值,当然不会按预期进行比较。