2014-03-30 136 views
2

我正在嘗試做一個do-while循環,使得程序在用戶輸入「y」或「Y」後再次迭代,但每次運行程序時,它都會打印出來是這樣的:while循環在第一個循環後沒有正確打印

 
Would you like to try again? 
Enter Y for yes or N for no: [DrJava Input Box] (I enter y) 
Are you a student? (no input box is shown, and it skips it) 
Are you a member of staff or faculty? [DrJava Input Box] (i enter yes or no) 
How many tickets do you need? [DrJava Input Box] (I enter an int, but it doesnt complete that part where it shows how many tickets sold or how much it costs) 
Would you like to try again? 
Enter Y for yes or N for no: [DrJava Input Box] 

這是我的計劃是什麼樣子:

import java.util.Scanner; 

public class Ticket 
{ 
    public static void main(String[] args) 
    { 
     Scanner keyboard = new Scanner(System.in); 

     double ticketprice = 12.00; 
     double result; 
     double result2; 
     double result3; 
     char repeat; 
     String input; 
     String student; 
     String staff; 
     int ticket; 

     do 
     { 
      System.out.println("Are you a student?"); 
      student = keyboard.nextLine(); 


      System.out.println("Are you a member of staff or faculty?"); 
      staff = keyboard.nextLine(); 


      System.out.println("How many tickets do you need?"); 
      ticket = keyboard.nextInt(); 



      if(student.equals("yes")) 
      { 
       System.out.println("Here is your " + ticket + " tickets for $0.00"); 
       } 


      if(staff.equals("yes")) 
      { 
       result = ticketprice * .85; 
       result2 = ticket * result; 
       System.out.printf("Here are your " + ticket + " tickets for $%.2f\n", result2); 
       } 



      if(student.equals("no") && staff.equals("no")) 
      { 
       result3 = ticket * ticketprice; 
       System.out.printf("Here are your " + ticket + " tickets, for $%.2f\n", result3); 
       } 


      System.out.println("Would you like to try again?"); 
      System.out.print("Enter Y for yes or N for no: "); 
       input = keyboard.next(); 
      repeat = input.charAt(0); 

     } 
     while(repeat == 'y' || repeat == 'Y'); 

    } 
} 

我是一個初學者到編程,所以任何幫助將是一件好事。謝謝。

+0

[使用後nextInt跳躍nextLine()()(可能重複http://stackoverflow.com/questions/13102045/skipping-nextline -after使用-nextint) –

回答

0

在循環結束時,您可以撥打next()來讀取「再試一次」響應。這會讀取下一個標記,但仍然將行結束於輸入流。

通過循環的下一次,當您撥打nextLine()來讀取「你是學生」的迴應時,它會立即讀取該行的其餘部分。

最簡單的解決方法是:

  • 使用nextLine(),而不是next()爲貴「重試」的提示,但那麼這意味着你將不得不走線的照顧終止nextInt()在「剩票「的問題,這樣的話你會有...
  • 使用nextLine(),而不是nextInt()您的‘入場券’的問題,然後使用Integer.parseInt()將字符串解析成一個int。

的替代選項,因爲所有你的反應似乎只是單個詞的反應,就是用next()/nextInt()無處不在,而不是使用nextLine()在所有。關鍵是,當你混合兩者時,你必須意識到它們如何相互作用。

+0

看到我試過的,可是當我做了我會得到看起來像這樣的錯誤。 java.lang.StringIndexOutOfBoundsException:字符串索引超出範圍:0 \t在java.lang.String.charAt(未知來源) \t在Ticket.main(Ticket.java:59) \t在sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) \t at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) \t at sun.reflect.DelegatingMethodAccessorImpl。調用(未知來源) \t在java.lang.reflect.Method.invoke(未知來源) \t在edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272) – user3479350

+0

@ user3479350對不起,請參閱編輯。 'nextInt()' - >'nextLine()'會產生同樣的問題。你將不得不再做一次改變。 –

+0

@ user3479350是的,這會解釋錯誤,因爲'nextInt()'也只是消耗整數而使行結束,然後'nextLine()'讀取長度爲0的空字符串(行的其餘部分),然後' .charAt(0)'因此超出範圍。順便提一下,在嘗試訪問'.charAt(0)'之前,您可能還想檢查以確保該字符串中至少有一個字符。即使在解決問題之後,如果用戶只輸入「enter」而不輸入任何內容,也不希望它崩潰。 –

0

問題在於這個代碼塊:

 System.out.println("Would you like to try again?"); 
     System.out.print("Enter Y for yes or N for no: "); 
     input = keyboard.next(); 
     repeat = input.charAt(0); 

當你調用keyboard.next(),它不讀取整行,所以當你的程序再次循環,下一次你打電話keyboard.nextLine(),它需要你在循環中先前輸入的內容的一部分,並且它自己超前。

最好的解決方案是在你調用keyboard.next()之後添加一個keyboard.nextLine(),這樣該行的其餘部分將被消耗,並且不會被遺留下來,以免混淆未來迭代的循環。

因此,例如,你可以改變這樣的:

 System.out.println("Would you like to try again?"); 
     System.out.print("Enter Y for yes or N for no: "); 
     input = keyboard.next(); 
     keyboard.nextLine(); 
     repeat = input.charAt(0);