2015-11-03 88 views
2

當我要求教師輸入時,它將正確執行,直到打印輸出爲止,然後打印請求800號碼(僅向學生要求)。我無法弄清楚爲什麼在你輸入了教員之後,他總是問學生的具體問題。這兩個分支都在我的if-else語句中執行

public class UniversityIDApp { 
public static void main(String[] args){ 
    // display a welcome message 
    System.out.println("Welcome to the University ID application"); 
    System.out.println(); 

    Scanner sc = new Scanner(System.in); 
    String choice = "y"; 
    while(choice.equalsIgnoreCase("y")) 
    { 
     // get user entries 
     String userType = Validator.getString(sc, 
      "Create student or faculty? (s/f): ", "s", "f"); 
     String firstName = Validator.getString(sc, 
      "Enter first name: "); 
     String lastName = Validator.getString(sc, 
      "Enter last name: "); 
     String email = Validator.getString(sc, 
      "Enter e-mail: "); 


    if (userType.equalsIgnoreCase("f")) 
    { 

    String department = Validator.getString(sc, 
      "Enter department: "); 

     // create faculty object 
     Faculty f = new Faculty() {}; 
     f.setFirstName(firstName); 
     f.setLastName(lastName); 
     f.setEmail(email); 
     f.setDepartment(department); 
     System.out.print("Southern Illinois University Faculty "); 
     System.out.print("\nName:  " + f.getFirstName() + " " + f.getLastName() + "\n" 
     + "Email:  " + f.getEmail() + "\n" + "Department: " + f.getDepartment() + "\n"); 
     System.out.println(); 
    } 
    else if(userType.equalsIgnoreCase("s")); 
    { 

    Student s = new Student() {}; 
    String student800Number = Validator.getString(sc, 
      "Enter your 800 number: "); 

     s.setFirstName(firstName); 
     s.setLastName(lastName); 
     s.setEmail(email); 
     s.setStudent800Number(student800Number); 
     System.out.print("Southern Illinois University Student"); 
     System.out.print("\nName:  " + s.getFirstName() + " " + s.getLastName() + "\n" 
    + "Email:  " + s.getEmail() + "\n" + "Student 800 Number: " + s.getStudent800Number() + "\n"); 
     System.out.println(); 

     } 
     System.out.print("Continue? (y/n): "); 
     choice = sc.next(); 
     System.out.println(); 
    } 
} 
+9

刪除能把那個分號後面的else if條件。 – rgettman

+0

Eric Lippert的C#遺憾列表中的#10(推理也適用於Java)http://www.informit.com/articles/article.aspx?p=2425867 –

回答

0

else if子句分號;使得它什麼也不做。

... 
} 
    else if(userType.equalsIgnoreCase("s")); //<-- here 
{ 
... 

這是與此相同:

 if (true) { 
      System.out.println("true"); 
     } else if (false) { 
      ; 
     } 

     { 
      System.out.println("false? what's going on?"); 
     } 

這可能已經比較容易,如果你把在同一行左括號(這是Java的約定)