2013-12-03 70 views
0

由於某種原因,我不知道,當我的代碼循環回到開始一些字符串輸入選項丟失..這是我的問題。當代碼從一個循環重複時,用戶輸入選項丟失

Enter student's name: 
Bob 
Enter student's subject: 
Maths 
Enter student's number: 
12345 
Enter level student last completed (0-3): 
0 
Would you like to enter data for another student? Yes/No 
Yes 
Enter student's name: 
Enter student's subject: 
English   
Enter student's number: 
55677 
Enter level student last completed (0-3): 
0 

當環路繞着再次允許用戶再次輸入信息,它只是打印出

Enter student's name: 
Enter student's subject: 
English   
Enter student's number: 
55677 
Enter level student last completed (0-3): 
0 

同時,只允許輸入的主題不是這個名字,你可以看到,跳過學生姓名的輸入選項。之後的領域正常工作。

這裏是我的代碼

import java.util.*; 

public class StudentData 
{ 
    public static void main(String[] args) 
    { 
     Scanner in = new Scanner(System.in); 
     ArrayList<Student> studentList = new ArrayList<Student>(); 
     String yesNo; // = "true"; 
     int level = 0; 

     do 
     { 
      int averageResult = 0; 
      int result1 = 0; 
      int result2 = 0; 
      int result3 = 0; 

//   in.next(); 
      System.out.println("Enter student's name: "); 
      String name = in.nextLine(); 
      System.out.println("Enter student's subject: "); 
      String subject = in.nextLine(); 
      System.out.println("Enter student's number: "); 
      int number = in.nextInt(); 

      do 
      { 
       System.out.println("Enter level student last completed (0-3): "); 
       level = in.nextInt(); 
       } 
      while (level > 3 || level < 0); 
      if (level > 0) 
      { 
       if (level == 3) 
       { 
       System.out.println("Enter result for level 3: "); 
       result3 = in.nextInt(); 
       System.out.println("Enter result for level 2: "); 
       result2 = in.nextInt();    
       System.out.println("Enter result for level 1: "); 
       result1 = in.nextInt(); 
       } 
      else if (level == 2) 
      { 
       System.out.println("Enter result for level 2: "); 
       result2 = in.nextInt(); 
       System.out.println("Enter result for level 1: "); 
       result1 = in.nextInt(); 
       } 
      else if (level == 1)  
      { 
       System.out.println("Enter result for level 1: "); 
       result1 = in.nextInt(); 
       } 

//   averageResult = 0; 
      averageResult= ((result1 + result2 + result3)/level); 
      } 
//   System.out.println(averageResult); 
      else 
      { 

      } 
      Student s = new Student(name,subject,number,level,result1,result2,result3,averageResult); 
      studentList.add(s); 



      do 
      { 
       System.out.println("Would you like to enter data for another student? Yes/No "); 
       yesNo = in.next(); 
       } 
      while (!yesNo.equalsIgnoreCase("YES") && !yesNo.equalsIgnoreCase("NO")); 
      } 
     while (yesNo.equalsIgnoreCase("YES")); 




     for(int i = 0; i < studentList.size(); i++) 
     { 
      System.out.println("Student's name: "+studentList.get(i).getName()); 
      System.out.println("Student's subject: "+ studentList.get(i).getSubject()); 
      System.out.println("Student number: "+studentList.get(i).getNumber()); 
      System.out.println("Level which student has completed: "+studentList.get(i).getLevel()); 
      System.out.println("Result for level 1:"+studentList.get(i).getResults1()); 
      System.out.println("Result for level 2:"+studentList.get(i).getResults2()); 
      System.out.println("Result for level 3:"+studentList.get(i).getResults3()); 
      System.out.println("Average result for all completed levels: "+studentList.get(i).getAverageResults()); 
      System.out.println(); 
      } 
     } 
    } 

我怎麼能允許輸入學生姓名,一旦循環? 謝謝

回答

0

做如下修改,它會如預期

Scanner in; //do not create Scanner instance here 
ArrayList<Student> studentList = new ArrayList<Student>(); 
String yesNo; // = "true"; 
int level = 0; 

do 
{ 
    in = new Scanner(System.in);//instead create here 
0

我有同樣的問題,我認爲這是掃描儀的nextLine()方法的問題。 解決這個問題的一個簡單方法是使用nextLine()方法來複制遇到問題的地方,或者您可以使用另一個對象從用戶處獲取輸入。

0

而不是使用nextLine()獲取的名稱和主題,你可以使用next()將解決您的問題