java - 当我运行 java 应用程序时遇到问题,它无法按我的预期执行

标签 java

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

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

最佳答案

您需要将室友放入一卷中,例如这样:

roomA.setRoomMate(newRoommate)

关于java - 当我运行 java 应用程序时遇到问题,它无法按我的预期执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60195040/

相关文章:

java - Rx 中的 groupBy、过滤器和内存泄漏

java - 如何使用spring初始化、加载属性和处置我自己的连接池?

java - 从通用列表java中删除项目?

java - 检查 boolean 条件,哪种方法更好

java - 白标错误页面 - 应用程序没有明确的映射/错误

java - 使用枚举值进行切换

java - Head First 设计模式 : Decorator behavior puzzling

java - Android - 无法扩展 ImageView

Java:如何使用 ObjectOutputStream 将静态文本添加到文件

Java Util Logger 写入同步