我真的不知道,而我的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));
}
}
}
如果有人能告訴我是什麼問題,那將是巨大
你確信它擊中while循環在所有做? – 2014-11-22 03:07:38
不,它似乎在跳過reply = s.nextLine(),因爲它甚至沒有給我輸入任何東西的機會。我不知道爲什麼它會這樣做。 – pittcrew 2014-11-22 03:09:37