java 我的布尔值由于某种原因被指定为false时被设置为true

e3bfsja2  于 2022-12-10  发布在  Java
关注(0)|答案(1)|浏览(191)

我尝试做一个控制台,你可以通过说yes或no来改变一个变量,由于某种原因,我的一个布尔(自动)变量被设置为true,即使它应该默认为false,下面是代码:

import java.util.Scanner;
public class Digital {
    public static void main(String[] args) {

        boolean ignite = false;
        boolean heatup = false;
        boolean dispose = false;
        boolean alertwick = false;
        boolean auto = false;

        boolean candlewick = false;
        boolean off = false;
        boolean ready = false;
        boolean swich = false;

        System.out.println("consol- set circumstances of the candle");
        Scanner myObj = new Scanner(System.in);
        System.out.println("is the switch on?");

        String consolswich = myObj.nextLine();

        if (consolswich.equals("yes")) {

            swich = true;
        } else {

        }


        System.out.println("is the candle ready?(in auto)");

        String consolready = myObj.nextLine();
        if (consolready.equals("yes")) {
            ready = true;
        } else {

        }
        System.out.println("is auto on?");

        String consolau = myObj.nextLine();
        if (consolau.equals("yes")) {
            auto = !auto;
        } else {

        }
        System.out.println("are there any wicks left?");
        String consolwick = myObj.nextLine();
        if (consolwick.equals("yes")) {
            candlewick = true;
        } else {

        }

        System.out.println("how long is the candlewick?(whole number, cm)");
        int consollength = myObj.nextInt();
        int candleheight = consollength;



        if (auto = true) {
            System.out.println("ballin");

            if ((swich = true) && (auto = false)) {
                if (candleheight < 1) {
                    dispose = true;
                    System.out.println("candlewick disposed");
                }
                if (candlewick = false) {
                    alertwick = true;
                    System.out.println("user as been alerted to the lack of candlewicks");

                } else {
                    heatup = true;
                    System.out.println("the candle has been formed and then ignited");

                }

            } else {
                System.out.println("nothing happened/error");
            }
        } else {
            while (auto = true) {

                if (ready = true)

                {
                    ignite = true;
                    System.out.println("the candle has been lit");
                    break;

                } else {
                    if (candleheight == 0) {
                        dispose = true;
                        System.out.println("candlewick has been disposed of");
                    }
                    if (candlewick = false) {
                        alertwick = true;
                        System.out.println("user has been alerted to the lack of candlewicks");
                        break;
                    } else {
                        System.out.println("the candle has been formed");
                        ready = true;
                    }
                }
            }
        }



    }
}

我已经尝试了不同的方法来改变布尔值,我知道所有其他部分的工作,其他变量的工作时,自动被删除,但它是一个必要的一部分,所以我不能删除它的程序

c6ubokkw

c6ubokkw1#

考虑在while()if()语句中使用比较==,因为您要比较两个变量。
运算符=用于赋值,将左边变量的值设置为右边的值。
另一个要考虑的改进,但考虑直接使用布尔变量,而不是与truefalse进行比较。例如,尝试转换:

if ((swich = true) && (auto = false)) {
    // more code
}

if(switch && !auto){
    // more code
}

这将使if()语句的目的更加清楚。

相关问题