Java 是否可以以这种方式使用扫描仪?

Java 是否可以以这种方式使用扫描仪?,java,Java,我正在编程一些东西,创建多个教师对象: public class Teacher { // 1) Define instance variables String teacherName; String catchPhrase; public static int roomNum; // 2) Write the constructor method public Teacher() { teacherName = "unknown"; catchPhrase = "unkn

我正在编程一些东西,创建多个教师对象:

public class Teacher {

// 1) Define instance variables
String teacherName;
String catchPhrase;
public static int roomNum;
// 2) Write the constructor method

public Teacher() {
    teacherName = "unknown";
    catchPhrase = "unknown";
    roomNum = 0;
}//end no-arg constructor

public Teacher(String newTeacher, String newCatch, int newRoom) {
    teacherName = newTeacher;
    catchPhrase = newCatch;
    roomNum = newRoom;
}



// 3) Write the proccessing methods (getters and setters)

public void setName(String newName) {
    teacherName = newName;
}

public String getName() {
    return teacherName;
}

public static int getRoom() {
    return roomNum;

}

// 4) Write the out method (eg toString() method)

public String toString() {

    String str = "Name: " + teacherName + ". \nCatch phrase: " + catchPhrase + " \nRoom number: " + roomNum + ".";

    return str;

}//end toString

public static void main(String args[]) {

}
}
它是这样执行的:

Teacher teacherName("First Last", "Catch Phrase", 123); 
if (rooms.contains(input)){
            System.out.println("That teacher is in our database!");
            //System.out.println(new int[(rooms).indexOf(1)]);
        } else {
        System.out.println("Sorry, that teachner was not found in our database!");
    }
我有多个教师对象。我正在尝试制作一个扫描仪,用于检查用户输入的数据,以查看输入的数字是否是其中一个对象的房间号:

 while (input != -1) {
        Scanner scan = new Scanner(System.in);
        input = scan.nextInt();
            if(input == Teacher.getRoom()) {
                System.out.println("Yes");
            } else if(input != Teacher.getRoom()) {
                System.out.println("Nope");
            }

        }
但我不知道怎么做。或者如果可能的话

任何帮助都将不胜感激

谢谢大家!

编辑:

我尝试了另一种方法。我尝试使用一个带有房间号的数组,并将其和输入进行比较,但并没有成功

int[] rooms = {220, 226, 204, 234, 236, 242, 243, 129, 125, 136, 101, 104, 107, 113, 103, 105, 102, 108, 117, 111, 111, 313, 310, 132, 127, 129, 125, 
            + 124, 122, 126, 130, 137, 114, 138, 136, 123, 135, 128, 139, 134, 220, 215, 211, 222, 253, 213, 252, 231, 255, 224, 254, 
            + 218, 235, 233, 000, 212, 223, 257, 217, 259, 214, 240, 258, 221, 210, 219, 256, 216, 110, 133, 115, 423, 253, 230, 115, 106, 1062, 418, 415};

 if (rooms.equals(input)) {
            System.out.println("Yes");
        } else {
            System.out.println("Nope");
        }
那没用。也没有:

 if (Arrays.asList(rooms).contains(input)) {
            System.out.println("Yes");
        } else {
            System.out.println("Nope");
        }
如果您能在整数数组(或更好的方法)中使用此函数,将不胜感激

多谢各位

编辑2:

我让它像这样工作:

Teacher teacherName("First Last", "Catch Phrase", 123); 
if (rooms.contains(input)){
            System.out.println("That teacher is in our database!");
            //System.out.println(new int[(rooms).indexOf(1)]);
        } else {
        System.out.println("Sorry, that teachner was not found in our database!");
    }

多谢各位

最简单的方法是创建一个
Teacher
数组,然后在while循环中放置一个for循环,该循环遍历所有
Teacher
对象并检查房间号。 或者更好的办法是,制作一个只包含房间号码的数组,并在其中循环

另外,您可能希望在while循环之外实例化扫描仪,然后执行
while(scan.hasNextInt()){…

所以会是这样的

Scanner scan = new Scanner(System.in);
Teacher[] teachears...
while (scan.hasNextInt()) {
    input = scan.nextInt();
    boolean isARoom = false;
    for(Teacher teach : teachers){
        if(input == teach.getRoom()) {
            isARoom = true;
        } 
    }
    if(isARoom)
        System,out.println("Yes");
    else
        System,out.println("No");  
 }

roomNum
不应为静态!静态表示所有教师共享一个房间号。您希望每位教师都有自己的房间号。