2014-11-22 4 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

당신은이 할 일에 모든 루프 동안 안타 것을 확신? –

+0

아니, 답장 = s.nextLine()을 건너 뛰고있는 것 같습니다. 아무 것도 입력 할 기회조차주지 못하기 때문입니다. 나는 이것이 왜 이렇게하고 있는지 전혀 모른다. – pittcrew

답변

1

문제는이 라인이다 좋은 것입니다 무엇인지 말해 줄 수 있다면

double tempPrice = s.nextDouble(); 

그것은 단지 그 이후 double 값이 아닌 줄 바꿈을 읽고, 당신의 개행은 nextDouble() 후에 nextLine()에 의해 읽힐 것입니다. 방지하기 위해 그 줄 바꿈을 읽을 수 s.nextDouble()s.nextLine()를 추가

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

이 작업을 해 주셔서 감사합니다. – pittcrew

+0

답변을 수락 된 것으로 표시해야합니다 –

관련 문제