2013-08-21 20 views
0

在一個學習程序中(我對這個知之甚少,它是學習java的),輸出不會停下來在我有方法的第二次迭代時得到輸入稱爲getQ,因爲這是一個酒吧測驗。在方法的第二次重複中跳過輸入

這是代碼:

import java.util.Scanner; 
public class pubQuizArray { 
private static Scanner kb = new Scanner (System.in); 
static String[] questions; 
static String[][] answers; 
static char ans; 
static char yn; 
static char[] correctAns; 
static int questionNum; 
static int questionNumArray; 
static int numQ; 
static int score; 

public static void writeQuiz() 
{ 
    getQNum(); 
    getQ(); 
} 

public static void getQNum() 
{ 
    System.out.println("How many Questions?"); 
    numQ = kb.nextInt(); 
    questions = new String[numQ]; 
} 

public static void getAns() 
{ 
    answers = new String[numQ][6]; 
    System.out.println("What are the answers?"); 

    System.out.print("a: "); 
    answers[questionNum][0] = kb.nextLine(); 

    System.out.print("b: "); 
    answers[questionNum][1] = kb.nextLine(); 

    System.out.print("c: "); 
    answers[questionNum][2] = kb.nextLine(); 

    System.out.print("d: "); 
    answers[questionNum][3] = kb.nextLine(); 


    correctAns = new char[numQ]; 
    System.out.println("What is the correct Answer?"); 
    correctAns[questionNum] = kb.next().charAt(0); 

} 

public static void getQ() 
{ 
    questionNum = 0; 
    System.out.println("What is the First Question?"); 
    questions[questionNum] = kb.nextLine(); 
    questions[questionNum] = kb.nextLine(); 
    getAns(); 
    questionNum ++; 
    while(questionNum < numQ) 
    { 
     System.out.println("What is the next Question?"); 
     questions[questionNum] = kb.nextLine(); 
     getAns(); 
     questionNum ++; 
    } 
} 

public static void askQ() 
{ 
    questionNum = 0; 
    score = 0; 
    do 
    { 
     System.out.println("Q" + (questionNum + 1) +": " + questions[questionNum]); 

     System.out.println("a: " + answers[questionNum][0]); 
     System.out.println("b: " + answers[questionNum][1]); 
     System.out.println("c: " + answers[questionNum][2]); 
     System.out.println("d: " + answers[questionNum][3]); 

     ans = kb.next().charAt(0); 
     if(ans == correctAns[questionNum]) 
     { 
      System.out.println("That was correct"); 
      score ++; 
     } 
     else 
     { 
      System.out.println("That was incorrect"); 
     } 
     questionNum ++; 
    }while(questionNum < numQ); 
} 

public static void menu() 

{ 
    System.out.println("Would you like to write a new Quiz? y/n"); 
    yn = kb.next().charAt(0); 
    while(yn == 'y') 
    { 
     writeQuiz(); 
     System.out.println("Would you like to play the Quiz? y/n"); 
     yn = kb.next().charAt(0); 
     while(yn == 'y') 
     { 
      askQ(); 
      System.out.println("Would you like to play again? y/n"); 
      yn = kb.next().charAt(0); 
     } 
    } 
} 

public static void main(String[] args) 
{ 
    menu(); 
} 
} 

,這是輸出

Would you like to write a new Quiz? y/n 
y 
How many Questions? 
2 
What is the First Question? 
asdf 
asdf 
What are the answers? 
a: asd 
b: as 
c: a 
d: adfs 
What is the correct Answer? 
a 
What is the next Question? 
What are the answers? 
a: 

,你可以看到,當問第二個問題是它不允許的輸入。 請記住,這只是一個學習java的項目。

+0

雖然這不是完全重複的,但是對於'Scanner.next()'的行爲是相同的。或者,您可以用'kb.nextLine()。charAt(0);'替換每個'kb.next()。charAt(0);'。那麼你不會面對這個問題。 –

回答

0

我不能完全確定是什麼原因造成這一點,但看着javadoc註釋

Advances this scanner past the current line and returns the input that was skipped. This 
method returns the rest of the current line, excluding any line separator at the end. The 
position is set to the beginning of the next line. 

我的猜測是,當nextLine()被調用的一條線,它試圖在返回任何東西存在上一行(不包括換行符)並將掃描器放在下一行。

我試圖打印由nextLine字符串返回的長度()調用,它是0

只是爲了檢查是否真的返回任何東西,我提供的輸入作爲

What is the correct Answer? 
a b 

和nextLine()調用返回「b」字符串(注意空間是必需的,否則整個輸入將被視爲一個標記,並將被kb.next()讀取)。

要解決您的問題,可以在用戶閱讀答案時使用kb.nextLine()。chatAt(0)。

System.out.println("What is the correct Answer?"); 
correctAns[questionNum] = kb.nextLine().charAt(0); 

希望這會有所幫助。

+0

謝謝你已經完成了,現在它工作。好極了 – user2682894

相關問題