布尔,if-else语句和扫描仪?

Shi*_*rla -1 java if-statement boolean object java.util.scanner

我试图在一个问题的真实陈述中做出"是"(或在这种情况下为"y").对以下问题的任何其他回应都被称为虚假陈述.

System.out.print("Do you smoke?(y/n): ");
        boolean smoker = console.nextBoolean();
        if (smoker.equalsIgnoreCase("y")) {
            smoker = true;
        } else {
            smoker = false;
        }
Run Code Online (Sandbox Code Playgroud)

我收到了错误

HealthPlan.java:32: error: boolean cannot be dereferenced
        if (smoker.equalsIgnoreCase("y")) {
              ^
Run Code Online (Sandbox Code Playgroud)

有谁知道我怎么解决这个问题?我在网上搜索过,我不太确定.

Lau*_*ens 7

不能投了String一个boolean,搭上console.next线的String.检查它是否为y,然后将该值放在布尔值中

String smoker = console.nextLine();
boolean isSmoker = false;
if (smoker.equalsIgnoreCase("y")) {
    isSmoker = true;
}
Run Code Online (Sandbox Code Playgroud)