在第二次重复方法时跳过输入

时间:2013-08-21 16:34:30

标签: java debugging

在学习程序中(我对此不太了解。这是学习java)输出不会停止在我称之为getQ的方法的第二次迭代中获得输入,因为这是一个pub测验。

这是代码:

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的项目。

1 个答案:

答案 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);

希望这会有所帮助。