2013-08-21 2 views
0

학습 프로그램에서 (나는 이것에 대해 많이 모른다. 자바를 배웠다.) 출력은 내가 가지고있는 메서드의 두 번째 반복에서 입력을 얻는 것을 멈추지 않는다. 이것은 펍 퀴즈이기 때문에 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: 

입니다. 자바를 배우기위한이 프로젝트를 기억하십시오.

+0

정확한 복제는 아니지만 'Scanner.next()'의 동작은 동일합니다. 또는 각 kb.next(). charAt (0);를 kb.nextLine(). 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. 

내 생각은, 꽵()가 라인 사이에 호출 될 때, 그것은에있는 아무것도 반환하려고한다 이전 줄 (줄 바꿈 문자 제외)을 입력하고 스캐너를 다음 줄에 놓습니다.

내가 전화) (꽵에 의해 문자열 반환의 길이를 인쇄 시도하고 그냥 정말 아무것도 반환 여부를 확인하기 위해

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

관련 문제