因此,我有一个带有rooms的arrayList,并让用户将其放置在他们要查找的房间中,im将其捕获为字符串,然后尝试使用instanceOf将其与java类的名称进行匹配,但无法这样做是由于将字符串与Java类进行比较。
我还在switch语句中捕获了答案,以确保这些类的拼写完美无误。只是不确定如何到达arrayList并拉出他们要查找的类。
public static void serachRooms(ArrayList<Room> rooms) {
int option = 0;
String temp = "";
boolean flag = false;
do {
System.out.println("please Enter What room Type you would like:"
+ "\nNormal Room = 1"
+ "\nComputer Room = 2"
+ "\nBiology Lab = 3"
+ "\nBoard Room = 4"
+ "\nYou must choose one!");
option = input.nextInt();
if (option == 1 || option == 2 || option == 3 || option == 4) {
flag = true;
}
} while (!flag);
switch (option) {
case 1:
temp = "BiologyLab";
break;
case 2:
temp = "BoardRoom";
break;
case 3:
temp = "ComputerRoom";
break;
case 4:
temp = "Room";
break;
}
for (Room room : rooms) {
if (temp instanceof BiologyLab) {
}
}
}
答案 0 :(得分:1)
instanceof
是非常特殊的,与类型和继承有关。签出Java documentation。
对于您的情况,您想将字符串temp
与字符串"BiologyLab"
进行比较。只需使用
if ("BiologyLab".equals(temp)) {
...
}
并查看How To Compare Strings In Java以获得更多信息。