gpt4 book ai didi

java - put 语句后 map 未更新

转载 作者:行者123 更新时间:2023-12-02 04:55:37 25 4
gpt4 key购买 nike

好吧,正如标题所说,我真的不明白为什么我的 map 没有更新。

我有一家有房间的“酒店”。每个房间都可以有一个顾客。酒店房间一开始就被填充,并给他们 key (1-10)和客户(最初为空)

public class Hotel {

public Map<Integer, Room> map = new HashMap<>();

public static void main(String[] args) {
new Hotel().run();
}
public void run() {
// create rooms
createRooms();
// add customer
addCustmer(2, "Plastman");

showRooms();

}
public void showRooms() {
for (Map.Entry<Integer, Room> entry : map.entrySet()) {
Integer roomNumber = entry.getKey();
Room room = entry.getValue();
System.out.println("Room: " + roomNumber + " Customer " + room.getCutomer() );
}
}
public void addCustmer(Integer roomNumber, String customer) {
// check if room is empty.
Room room = map.get(roomNumber);
if(room.getCutomer() == null || room.getCutomer().equals("")) {
// room is vacant.
System.out.println("Room is vacant. Adding customer: " + customer);
map.put(roomNumber, new Room(customer));
} else {
// room is NOT vacant.
System.out.println("Room is NOT vacant");
}
}
private void createRooms() {
for(int i = 1; i < 11; i ++) {
map.put(i,new Room(""));
}
}

最佳答案

在您的 room 方法中,您需要将参数设为小写。这可能就是问题所在。

public Room(String customer) {
setCustomer(customer);
}

关于java - put 语句后 map 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28799540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com