2014-09-26 95 views
1

我編程的東西,創建多個教師對象:以這種方式可以使用掃描儀嗎?

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

我有多個教師的對象。我試圖讓從用戶輸入檢查掃描儀,看看輸入的號碼是從這些對象中的一個房間號:

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

任何幫助獲得這與整數數組,(或更好的方法)的工作,將不勝感激。

謝謝。

EDIT2:

我得到它的工作是這樣的:

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!"); 
    } 

非常感謝您!

+0

'roomNum'不應是靜態的!靜態意味着所有教師共享一個房間號碼。你希望每位老師都有自己的房間號碼。 – 2014-09-26 05:31:11

回答

0

最簡單的方法是創建一個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"); 
}