简体   繁体   English

我在 Java 中的一个布尔方法没有返回 true,我不知道如何更正

[英]One of my boolean methods in Java is not returning true and i don't know how to correct that

Here is my code:这是我的代码:

public class SmartCard {

    private String name;

    //Constructor for SmartCard class
    public SmartCard(String name) {
        this.name = name;
    }
    //the getOwner() method which returns the owner's name
    public String getOwner() {
        return name;
    }

    //setStaff() method to set staff status
    public boolean setStaff(boolean status) {
            return true;
    }

    //isStaff() method returns true if card belongs to member of staff
    public boolean isStaff() {
        boolean staff;
        if (setStaff(true)) {
            staff = true;
        } else staff = false;
        return staff;
    }
}

public class Tester {

    public static void main(String... args) {
        testPart1a();
        testPart1b();
        testPart1c();`enter code here`
    }

    public static void testPart1a() {
        System.out.println("Part 1 - Accessor methods");
        System.out.println("======");

        System.out.println("--- Part 1a ---");
        System.out.println();

        System.out.println("* Creating a new SmartCard for student Anna Undergrad...");
        SmartCard card = new SmartCard("Anna Undergrad");
        System.out.println("Owner is: " + card.getOwner());
        System.out.println();
    }


    public static void testPart1b() {
        System.out.println("--- Part 1b ---");
        System.out.println();

        SmartCard card = new SmartCard("Anna Undergrad");

        System.out.println("Is " + card.getOwner() + " staff? " + card.isStaff());
        System.out.println();
    }



    public static void testPart1c() {
        System.out.println("--- Part 1c ---");
        System.out.println();

        System.out.println("* Creating a new SmartCard for staff member Dr. Bob Lecturer...");
        SmartCard card = new SmartCard("Dr. Bob Lecturer");
        card.setStaff(true);
        System.out.println("Is " + card.getOwner() + " staff? " + card.isStaff());
        System.out.println();
    }

When I run the program, my isStaff() method is always returning true when it should return false for Anna Undergrad and true for Bob Lecturer.当我运行程序时,我的 isStaff() 方法总是返回 true,而它应该为 Anna Undergrad 返回 false,而为 Bob Lecturer 返回 true。 Maybe I did it the wrong way and should change my setStaff() method.也许我做错了,应该改变我的 setStaff() 方法。

setters don't have to return boolean (unless you want to do something very unorthodox). setters不必返回boolean (除非你想做一些非常非正统的事情)。 Change it to:将其更改为:

//setStaff() method to set staff status
public void setStaff(boolean status) {
    this.status = status;
}

And change your getStaff() method like this:并像这样更改您的getStaff()方法:

public boolean isStaff() {
    return staff;
}

You will also need to define boolean staff :您还需要定义boolean staff

private String name;
private boolean staff;

...

I think your setStuff method is wrong :我认为你的 setStuff 方法是错误的:

public boolean setStaff(boolean status) {
        return true;
} 

And a simple note :还有一个简单的说明:
you can use getter and setter pattern for SmartCard :您可以对 SmartCard 使用 getter 和 setter 模式:

public class SmartCard {
    private String name ;
    private boolean stuff ;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isStuff() {
        return stuff;
    }

    public void setStuff(boolean stuff) {
        this.stuff = stuff;
    }

    @Override
    public String toString() {
        return "SmartCard{" +
                "name='" + name + '\'' +
                ", stuff=" + stuff +
                '}';
    }
}

And now test class :现在测试类:

class MainClass {
    public static void main(String[] args) {
        SmartCard card = new SmartCard() ;
        card.setName("Sample");
        card.setStuff(false);
        System.out.println(card);

        SmartCard card2 = new SmartCard();
        card2.setName("Other Card");
        card2.setStuff(true);
        System.out.println(card2);
    }
}

Looks like you need to declare a boolean Staff property and add it to the SmartCard constructor so you can set it and get it.看起来您需要声明一个布尔 Staff 属性并将其添加到 SmartCard 构造函数中,以便您可以设置并获取它。 Otherwise your setStaff method isn't setting anything, it's just returning true all the time as a signal that the method was run instead of getting a boolean value for Staff set in each SmartCard object.否则,您的 setStaff 方法不会设置任何内容,它只是一直返回 true 作为该方法已运行的信号,而不是获取每个 SmartCard 对象中设置的 Staff 的布尔值。

In other words set up a Staff boolean just like name where you can set it and get it.换句话说,像 name 一样设置一个 Staff 布尔值,您可以在其中设置并获取它。

暂无
暂无

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

相关问题 Java:我无法让它循环,布尔值有问题,但我不知道如何修复它 - Java: I can't get it to loop, somethings wrong with the boolean but I don't know how to fix it 回溯问题,我不知道我的解决方案是否正确 - Backtracking prorblem and I don't know if my plan to solve it is correct 布尔返回; 不知道如何返回我想要的结果 - Boolean return; don't know how to return result I want <true>但是是:<false> - 我不知道怎么解决</false></true> - <true> but was: <false> - i don't know how to solve it 我的代码出现错误,我不知道如何修复(Java fx stackPane) - I am getting errors in my code and i don't know how to fix(Java fx stackPane) 我不知道如何在macOS控制台JAVA中编写命令以显示文件的方向 - I don't know how to write a command to show direction to my file in macOS console, JAVA 我的Java版本不知道怎么安装JavaFX - Don't know how to install JavaFX for my Java version 我不知道我的一个变量有什么问题。[线程“main”中的异常java.util.UnknownFormatConversionException:Conversion =&#39;m&#39;] - I don't know what's wrong with one of my variable.[Exception in thread “main” java.util.UnknownFormatConversionException: Conversion = 'm'] 我的Java Chatbot代码不起作用,我也不知道为什么 - My Java Chatbot code don't work and i don't know why 我怎么知道我的代码库是否有 java 8 方法 - how do I know if my code base has java 8 methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM