[我在运行Java应用程序时遇到麻烦,但无法按预期执行

问题描述 投票:0回答:1

我的应用程序找不到我添加到房间的室友,请参见顺序:

Please select one of the following options:
Add a room in the apartment[1]
Search for a room in the apartment[2]
Add a roommate to an existing room[3]
Check if a room has a roommate[4]
Count the number of existing rooms that have roommates living in them[5]
3
What is the name of this roommate?
kack
What is the surname of thid roommata?
han
What is the age of this roommate?
20
Which room you want to add this roommate in?[A]/[B]/[C]
b
The roommate was added to the room!
Do you want to continue using the App? (Y/N)
y
Please select one of the following options:
Add a room in the apartment[1]
Search for a room in the apartment[2]
Add a roommate to an existing room[3]
Check if a room has a roommate[4]
Count the number of existing rooms that have roommates living in them[5]
4
What is the ID of the room?[A]/[B]/[C]
b
Sorry! There is no roommate in this room.

我不确定我必须在哪里重写。我想知道哪一部分是错误的,以及我应该如何重写我的代码。代码如下,第一部分是添加室友,第二部分是检查房间中是否有室友:

public boolean addRoommate(String pName, String pSurname, int pAge, char pID) {
    boolean response = false;

    if (roomA != null && roomA.getID() == (pID)) {
        Roommate newRoommate = new Roommate(pName, pSurname, pAge);
        response = true;

    } else if (roomB != null && roomB.getID() == (pID)) {
        Roommate newRoommate = new Roommate(pName, pSurname, pAge);
        response = true;

    } else if (roomC != null && roomC.getID() == (pID)) {
        Roommate newRoommate = new Roommate(pName, pSurname, pAge);
        response = true;
    }

    return response;
}

这是代码的第二部分,这是我检查室友的位置:

public Roommate checkRoommate(char pID) {
    Roommate response = null;

    if (roomA != null && roomA.getID() == (pID) && roomA.getRoommate() != null) {
        response = roomA.getRoommate();
    } else if (roomB != null && roomB.getID() == (pID) && roomB.getRoommate() != null) {
        response = roomB.getRoommate();
    } else if (roomC != null && roomC.getID() == (pID) && roomC.getRoommate() != null) {
        response = roomC.getRoommate();
    }

    return response;
}
java
1个回答
0
投票

例如,您需要将rommate放入rool中:

© www.soinside.com 2019 - 2024. All rights reserved.