2014-11-22 92 views
0

我真的不知道,而我的do-while循環不工作。每次運行程序時,它都會跳過它並繼續前進,甚至不需要回復。Java Do-while循環不工作?

import java.util.*; 

public class CourseApp { 
    public static void main(String[] args) { 

    Scanner s = new Scanner(System.in); 
    ArrayList <Course> courseList = new ArrayList<Course>(); 

    String reply; 
    //boolean boolVal = true; 

    do { 
     System.out.print("Enter new course number: "); 
     String tempCourseNum = s.nextLine(); 

     System.out.print("Enter course name: "); 
     String tempCourseName = s.nextLine();  

     System.out.print("Enter instructor's last name: "); 
     String tempLastName = s.nextLine(); 

     System.out.print("Enter instructor's first name: "); 
     String tempFirstName = s.nextLine(); 

     System.out.print("Enter instructor's username: "); 
     String tempUsername = s.nextLine(); 

     //new Instructor(tempLastName, tempFirstName, tempUsername); 
     Instructor tempInstruc = new Instructor(tempLastName, tempFirstName, tempUsername); 

     System.out.print("Enter textbook title: "); 
     String tempTitle = s.nextLine(); 

     System.out.print("Enter textbook author: "); 
     String tempAuthor = s.nextLine(); 

     System.out.print("Enter textbook price (no $ sign): "); 
     double tempPrice = s.nextDouble(); 

     //new TextBook(tempTitle, tempAuthor, tempPrice); 
     TextBook tempText = new TextBook(tempTitle, tempAuthor, tempPrice); 

     courseList.add(new Course(tempCourseNum, tempCourseName, tempInstruc, tempText)); 

     System.out.print("\nEnter another course? (y/n): "); 
     reply = s.nextLine(); 

    } while (reply.equalsIgnoreCase("Y")); 

    for (int i =0; i < courseList.size(); i++) { 
     System.out.println("\n\tCourse #" + (i+1)+":"); 
     System.out.println(courseList.get(i)); 
    } 


} 

} 

如果有人能告訴我是什麼問題,那將是巨大

+0

你確信它擊中while循環在所有做? – 2014-11-22 03:07:38

+0

不,它似乎在跳過reply = s.nextLine(),因爲它甚至沒有給我輸入任何東西的機會。我不知道爲什麼它會這樣做。 – pittcrew 2014-11-22 03:09:37

回答

1

問題是此行

double tempPrice = s.nextDouble(); 

它只讀取後,一個雙精度值,而不是換行,您的換行符將在nextDouble()之後由nextLine()讀取。爲了防止加s.nextLine()s.nextDouble()後讀取換行符

double tempPrice = s.nextDouble(); 
s.nextLine(); 
+0

這工作,非常感謝你! – pittcrew 2014-11-22 03:12:09

+0

您應該將答案標記爲已接受 – 2014-11-22 03:56:26