2016-11-05 45 views
0

我在使用Java方面有點新鮮。我正在製作一個收錄學生姓名和身份證的程序,當之後輸入正確的身份證件時,會吐出該學生的信息。示例輸出將如下所示:您想輸入多少個學生? (2)他們的名字是什麼? (Sally,Jack)他們的ID是什麼? (2332,5631)你想搜索一個學生嗎? (Y)請輸入他們的ID:(2332)我們找到了Sally!圍繞嵌套if else語句進行循環

下面是代碼的片段,搜索回來的學生:

 System.out.println("Would you like to search for a student?"); 
     String answer = scan.next(); 

     if (answer.equals("Y")) { 
      System.out.println("Please enter an ID:"); 
      int id = scan.nextInt(); 
      boolean found = Student.lookupID(list, id); 

      if(found) 
       System.out.println("Student was found. This student is: " + studentName + ", Student ID " + id); //fix this 
      else 
       System.out.println("Error"); 
     } 
     else { 
      System.out.println("Thanks for using this system!"); 
    } 
} 

}

現在,我想循環的代碼,從而使輸出現在看起來像這個:你想輸入多少名學生? (3)他們的名字是什麼? (Sally,Jack,Rick)他們的ID是什麼? (2332,5631,3005)你想搜索一個學生嗎? (Y)請輸入他們的ID:(2332)我們找到了Sally!你想尋找另一名學生嗎? (Y)請輸入他們的ID:(5631)我們找到Jack!你想尋找另一名學生嗎? (N)感謝您使用我們的系統!

有人能幫助我嗎?

+0

你的問題不是關於Java明確。你試過的時候有什麼障礙? –

回答

0

需要重啓id來計劃能夠得到新的id.Try糾正你這樣的代碼:

System.out.println("Would you like to search for a student?"); 
String answer = scan.next(); 

int id = 0; //RESET THE ID TO BE ABLE REPLAYIN WORK! 
if (answer.equals("Y")) { 
    System.out.println("Please enter an ID:"); 
    id = scan.nextInt(); 
    boolean found = Student.lookupID(id); 

    if (found) 
     System.out.println("Student was found. This student is: " + studentName + ", Student ID " + id); // fix 
                              // this 
    else 
     System.out.println("Error"); 
} else { 
    System.out.println("Thanks for using this system!"); 
} 
+0

謝謝。你會知道如何循環它嗎?我正在嘗試使用一種做法。我遇到了麻煩。 @Coder ACJHP –

+0

**使用do while語句如下:**'do {// your codes} while(answer.equals(「Y」));'**這意味着只要正確的答案它將工作* * –