为什么执行代码跳过“食物在桌上”的第一条语句?[重复]

问题描述 投票:-1回答:1
public class ControlFlow {

 public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("Are you hungry?\n");
    String answer = input.nextLine();
    input.close();
    if(answer == "yes") {
        System.out.println("Food is on the table!");
    }else {
        System.out.println("Not hungry!\n");
     }

  }

}

我也尝试过使用布尔变量。它没有帮助

java
1个回答
0
投票

只需将字符串的内容与:

"yes".equals(answer)
© www.soinside.com 2019 - 2024. All rights reserved.