第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我在 Java 中的一個布爾方法沒有返回 true,我不知道如何更正

我在 Java 中的一個布爾方法沒有返回 true,我不知道如何更正

守著星空守著你 2021-11-24 15:13:54
這是我的代碼: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();    }當(dāng)我運(yùn)行程序時,我的 isStaff() 方法總是返回 true,而它應(yīng)該為 Anna Undergrad 返回 false,而為 Bob Lecturer 返回 true。也許我做錯了,應(yīng)該改變我的 setStaff() 方法。
查看完整描述

3 回答

?
慕碼人2483693

TA貢獻(xiàn)1860條經(jīng)驗 獲得超9個贊

setters不必返回boolean(除非您想做一些非常非正統(tǒng)的事情)。將其更改為:


//setStaff() method to set staff status

public void setStaff(boolean status) {

    this.status = status;

}

并getStaff()像這樣改變你的方法:


public boolean isStaff() {

    return staff;

}

您還需要定義boolean staff:


private String name;

private boolean staff;


...


查看完整回答
反對 回復(fù) 2021-11-24
?
慕桂英546537

TA貢獻(xiàn)1848條經(jīng)驗 獲得超10個贊

我認(rèn)為你的 setStuff 方法是錯誤的:


public boolean setStaff(boolean status) {

        return true;

還有一個簡單的說明:

您可以對 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 +

                '}';

    }

}

現(xiàn)在測試類:


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);

    }

}


查看完整回答
反對 回復(fù) 2021-11-24
?
幕布斯7119047

TA貢獻(xiàn)1794條經(jīng)驗 獲得超8個贊

看起來您需要聲明一個布爾 Staff 屬性并將其添加到 SmartCard 構(gòu)造函數(shù)中,以便您可以設(shè)置并獲取它。否則,您的 setStaff 方法不會設(shè)置任何內(nèi)容,它只是一直返回 true 作為該方法已運(yùn)行的信號,而不是獲取每個 SmartCard 對象中設(shè)置的 Staff 的布爾值。

換句話說,像 name 一樣設(shè)置一個 Staff 布爾值,您可以在其中設(shè)置并獲取它。


查看完整回答
反對 回復(fù) 2021-11-24
  • 3 回答
  • 0 關(guān)注
  • 259 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號