System.out.println("Please enter the number of the Basic room you would like to book : Basis Room 1, Basis Room 2, Basis Room 3 or Basic Room 4");
int rooms = input.nextInt();
int Key = rooms;
for (int i = 0; i<basicRooms.length;i++){
if (Key ==(basicRooms[i])){
System.out.println("Basic room " + rooms + " is empty"); break;
答案 0 :(得分:0)
下面是示例代码,它将使用房间号
存储客人姓名import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Map <Integer, String> basicRooms =new HashMap <Integer,String>();
basicRooms.put(1,"");
basicRooms.put(2,"");
basicRooms.put(3,"");
basicRooms.put(4,"");
Scanner input =new Scanner(System.in);
System.out.println("Please enter the number of the Basic room you would like to book : Basis Room 1, Basis Room 2, Basis Room 3 or Basic Room 4");
int rooms = input.nextInt();
int key = rooms;
for (int i = 0; i<=basicRooms.size();i++){
if (basicRooms.containsKey(key) ){
System.out.println("Basic room " + rooms + " is empty");
System.out.println("Please enter Guest name");
input.nextLine();
String guestName = input.nextLine();
basicRooms.put(key, guestName);
break;
}else{
System.out.println("Not Empty");
}
}
System.out.println(basicRooms);
}
}
答案 1 :(得分:0)
我还认为你应该使用HashMap.Room作为键和名称作为value。如果你想根据插入顺序显示Room,你可以使用LinkedHashMap.Include Room类属性是类代码气味,因为RoomName中的guestName将如果房间已注册Guest,则设置。如果没有为该房间注册的访客,则不会设置属性guestName,这将是额外的代码。这是代码味道。
答案 2 :(得分:-1)
使用hashmap。房间作为钥匙,名称为价值。