简体   繁体   English

if和else陈述

[英]if and else statement

I'm trying to implement a toString method, and the output of the toString depends on the boolean variables. 我想实现toString方法,以及输出toString依赖于boolean变量。 Below is my class and main. 以下是我的课程和主要内容。

public class Cell {

    public int addSpaces;
    boolean isEmpty;
    boolean isChute;
    boolean isLadder;

    public Cell() {
        addSpaces = 10; //I initialized addSpaces to 10 for testing purpose 
    }

    public boolean isChute() { //first boolean method
        if (addSpaces == -10) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isLadder() {//second boolean method
        if (addSpaces == 10) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isEmpty() { //third boolean method
        if (addSpaces == 0) {
            return true;
        } else {
            return false;
        }
    }

    public String toString() {
        String print;
        if (isChute = true) //if isChute is true return true.
        {
            print = "C10";       // toString output = "C10"
        } else if (isLadder = true) // if isLadder is true return true
        {
            print = "L10";          // toString output == "L10"
        } else {
            print = "---"; // else toString print output = "---"
        }
        return print;
    }

    public static void main(String[] arg) {
        Cell s = new Cell();

        System.out.println(s.addSpaces);
        System.out.println(s);
    }
}

Regardless of the input state of toString , I basically get the same output "C10". 无论toString的输入状态如何,我基本上都得到相同的输出“ C10”。

Can someone tell me what I did wrong? 有人可以告诉我我做错了什么吗?

I'm new to this website so I appreciate any feedback for future reference. 我是该网站的新手,非常感谢您提供任何反馈意见,以供将来参考。 Thank you. 谢谢。

You've fallen into one of the languages "gotchas" 您已经陷入一种语言“陷阱”

This... 这个...

if(isChute = true) //if isChute is true return true.
    print = "C10";       // toString output = "C10"
else if (isLadder = true) // if isLadder is true return true
    print = "L10";          // toString output == "L10"
else   
    print = "---"

is actually assigning true to isChute . 实际上将true分配给isChute You should be using == not = 您应该使用===

Updated 更新

A better approach would be... 更好的方法是...

if(isChute) //if isChute is true return true.
    print = "C10";       // toString output = "C10"
else if (isLadder) // if isLadder is true return true
    print = "L10";          // toString output == "L10"
else   
    print = "---"

If there are only two states that the object can be (either a chute or ladder), you could simply use 如果对象只能处于两种状态(溜槽或梯子),则可以简单地使用

if(isChute) //if isChute is true return true.
    print = "C10";       // toString output = "C10"
else print = "L10";          // toString output == "L10"

If it can have more then 2 states then I would use an enum type instead. 如果它可以有2个以上的状态,那么我将改用enum类型

isChute is assigned to true. isChute被分配为true。 So "C10" is being returned all the time by toString(). 因此,toString()始终会返回“ C10”。 Change it 更改

if(isChute){
    ...
}else if(isLadder){
    ...
}else{
    ..
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM