为什么if else语句在项目中不起作用?[重复]

问题描述 投票:-1回答:2

它总是说死了。有什么问题?

boolean death = false;
health = 100;

if (health < 0) {
    death = true;

    System.out.println("You are dead not a big suprise.");
} else {
    death = false;
}

if (death = false) {

    System.out.println("Living man.");
} else {

    System.out.println("Completely dead.");
}
java if-statement netbeans
2个回答
0
投票

如果你想使用 !=,你可以写 !dath 或 death==false。

public static void main(String[] args) {
            // TODO Auto-generated method stub
            boolean death = false;
            int health = 100;

            if (health < 0) {
                death = true;

                System.out.println("You are dead not a big suprise.");
            } else {
                death = false;
            }

            if (!death) {

                System.out.println("Living man.");
            } else {

                System.out.println("Completely dead.");
            }

        }

0
投票

用假的来检查死亡的有效性的条件是错误的。

if (death = false) {

这样一来,你就给死亡赋予了错误的价值。

改成 ==,因为我们用==来表示条件。

if (death ==  false) {

if (!death) {
© www.soinside.com 2019 - 2024. All rights reserved.