簡體   English   中英

我想問一下An Introduction to Object-Oriented Programming with Java(第5版)中的練習11(2級編程練習)

[英]I want to ask about exercise 11 (Level 2 Programming Exercises) in An Introduction to Object-Oriented Programming with Java (5th edition)

//我的擴展學生 class

public class ExtendStudent {
//data members
private String name;
private String email;

//constructor
public ExtendStudent(String StudentName, String StudentEmail){
    name = StudentName;
    email = StudentEmail;
}

//method
public String getName(){
    return name;
}

public String getEmail(){
    return email;
}

}

//我的擴展借書證class

public class ExtendLibraryCard {
//data members
private ExtendStudent owner;
private int numBorBooks;
private String expDate;
private int expDay;
private int expMonth;
private int expYear;
private boolean active;
private int thisYear;
private int thisMonth;
private int thisDay;

//constructor
public ExtendLibraryCard() {
    numBorBooks = 0;
    expDate = null;
    expMonth = 0;
    expYear = 0;
    setActive(true);
}

//methods
//set the owner be the student
public void setOwner(ExtendStudent student) {
    owner = student;
}

//get the name of the owner (which is the student)
public String getOwnerName() {
    return owner.getName();
}

//get the email of the owner
public String getOwnerEmail(){
    return owner.getEmail();
}

//number of books borrowed
public void totalBooksBorrowed(int totalBooks) {
        numBorBooks = numBorBooks + totalBooks;
}

//get number of books borrowed
public int getNumBorBooks() {
    return numBorBooks;
}

//print out
public String toStringExpDate() {
    return "Expiration Date: " + expDay + "/" + expMonth + "/" + expYear;
}

//expiration date
public void setExpDate(int expDay, int expMonth, int expYear) {
    this.expDay = expDay;
    this.expMonth = expMonth;
    this.expYear = expYear;
}

public int getExpMonth() {
    return expMonth;
}

public void setExpMonth(int expMonth) {
    this.expMonth = expMonth;
}

public int getExpYear() {
    return expYear;
}

public void setExpYear(int expYear) {
    this.expYear = expYear;
}

public int getExpDay() {
    return expDay;
}

public void setExpDay(int expDay) {
    this.expDay = expDay;
}

//set active
public void setActive(boolean state) {
    active = state;
}

//getter and setter of the current time
public int getThisYear() {
    return thisYear;
}

public void setThisYear(int thisYear) {
    this.thisYear = thisYear;
}

public int getThisMonth() {
    return thisMonth;
}

public void setThisMonth(int thisMonth) {
    this.thisMonth = thisMonth;
}

public int getThisDay() {
    return thisDay;
}

public void setThisDay(int thisDay) {
    this.thisDay = thisDay;
}

public void testing(){
    if(active = false)
        System.out.println("Your card is out of date. Please buy a new one or you will not be allowed to enter the library!");
    else
        System.out.println(toString());
}

//print out every single info to the card
public String toString(){
    return "Owner Name: " + getOwnerName() + "\n" +
            "Owner Email: " +getOwnerEmail() + "\n" +
            "Number of books borrowed: " + getNumBorBooks() + "\n" +
            "Today: " + getThisDay() + "/" + getThisMonth() + "/" + getThisYear() + "\n" +
            toStringExpDate();
}

}

//我的擴展圖書館員

public class ExtendLibrarian {
public static void main(String[] args) {
    ExtendStudent student = new ExtendStudent("SKT Faker", "fakerskt@yahoo.com");
    ExtendLibraryCard card = new ExtendLibraryCard();

    card.setOwner(student);
    card.getOwnerName();

    card.totalBooksBorrowed(20);

    card.setExpDate(20, 11, 2019);
    card.setThisDay(10);
    card.setThisMonth(11);
    card.setThisYear(2019);

    if (card.getThisYear() > card.getExpYear()) {
        card.setActive(false);
    } else if (card.getThisYear() == card.getExpYear()) {
        if (card.getThisMonth() > card.getExpMonth()) {
            card.setActive(false);
        }
    } else if (card.getThisYear() == card.getExpYear()) {
        if (card.getThisMonth() == card.getExpMonth()) {
            if (card.getThisDay() > card.getExpDay()) {
                card.setActive(false);
            }
        }
    }
    else {
        card.setActive(true);
    }

    card.testing();


}

}

所以事情就是說我的到期日是 2019 年 11 月 20 日,如果今天是 2019 年 11 月 21 日,代碼將打印出“您的卡已過期”,但事實並非如此。 有人可以幫我嗎,謝謝。

P/s: 對不起,如果我的英語很糟糕

首先在您的 ExtendLibraryCard Class 更改此行

if(active = false)

if (active == false)

同樣在您的主要圖書管理員 class 中,將條件從else if更改為if

if (card.getThisYear() > card.getExpYear()) {
        card.setActive(false);
    }
    if (card.getThisYear() == card.getExpYear()) {
        if (card.getThisMonth() > card.getExpMonth()) {
            card.setActive(false);
        }
    }
    if (card.getThisYear() == card.getExpYear()) {
        if (card.getThisMonth() == card.getExpMonth()) {
            if (card.getThisDay() > card.getExpDay()) {
                card.setActive(false);
            }
        }
    } else
        card.setActive(true);

    card.testing();
}

讓我給你試一下你的代碼中發生了什么:當你設置 thisYear= 2019 和 ExpYear= 2019 時,你的第一個 else if 語句得到滿足,因為card.getThisYear() == card.getExpYear() ,它沒有即使兩個月份相同,更大或更小也無關緊要(即是否滿足嵌套都無關緊要),因為您的 getThisYear等於ExpThisYear ,因此,如果滿足條件,則第一個 else 將不會t 檢查最后的 else if 條件(其中有您的日期檢查嵌套的 if 條件)。 因此 setActive(false) 沒有被執行。 也因為這個塊的 else if 條件

else if (card.getThisYear() == card.getExpYear()) {
        if (card.getThisMonth() > card.getExpMonth()) {
            card.setActive(false);
        }
    }

已經被執行並且它在第一部分返回true ,即使你的 else 塊也不會被執行。

因此,您有必要將您的條件更改為 if 語句,因為 else if 一旦條件已經滿足,將不會檢查與 if 不同的其他條件。

鏈接: 閱讀此內容以了解 if vs else if 條件 vs else

希望能幫助到你:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM