0

这是我一直在为一个简单的 pub 风格测验学习 Java 的一些代码。出于某种原因,它会第一次提出问题,然后从那时起跳过它。即使我对它们进行了初始化,它也将字符串声明为 null。

这是代码

    public class PubQuizWMethod
    {
        private static Scanner keyboard = new Scanner (System.in);
        static String a1,a2,a3,a4;
        static String b1,b2,b3,b4;
        static String c1,c2,c3,c4;
        static String d1,d2,d3,d4;
        static String ans1,ans2,ans3,ans4;
        static String q1,q2,q3,q4;
        static String question;
        static boolean newQuiz = true;
        static boolean notStop = true;
        static char correctAnswer;
        static char answer1,answer2,answer3,answer4;
        static char answer;
        static int score = 0;

public static void mainMenu()
{
    {
        {
            do{
                gettingQuestion();
                question = q1;

                gettingAnswers();
                ans1 = a1;
                ans2 = b1;
                ans3 = c1;
                ans4 = d1;

                gettingCorrectAnswer();
                answer1 = correctAnswer;



                gettingQuestion();
                question = q2;

                gettingAnswers();
                ans1 = a2;
                ans2 = b2;
                ans3 = c2;
                ans4 = d2;

                gettingCorrectAnswer();
                answer2 = correctAnswer;




                gettingQuestion();
                question = q3;

                gettingAnswers();
                ans1 = a3;
                ans2 = b3;
                ans3 = c3;
                ans4 = d3;

                gettingCorrectAnswer();
                answer3 = correctAnswer;




                gettingQuestion();
                question = q4;
                ans1 = a4;
                ans2 = b4;
                ans3 = c4;
                ans4 = d4;

                gettingCorrectAnswer();
                answer4 = correctAnswer;



                if(notStop == true)
                {
                    score = 0;

                    System.out.println(q1);

                    System.out.println("a: " +a1);
                    System.out.println("b: " +b1);
                    System.out.println("c: " +c1);
                    System.out.println("d: " +d1);

                    answer = keyboard.next().charAt(0);

                    if(answer == answer1)
                    {
                        System.out.println("That was correct");
                        score ++;
                    }
                    else
                    {
                        System.out.println("That was incorrect");
                    }



                    System.out.println(q2);

                    System.out.println("a: " +a2);
                    System.out.println("b: " +b2);
                    System.out.println("c: " +c2);
                    System.out.println("d: " +d2);

                    answer = keyboard.next().charAt(0);

                    if(answer == answer2)
                    {
                        System.out.println("That was correct");
                        score ++;
                    }
                    else
                    {
                        System.out.println("That was incorrect");
                    }



                    System.out.println(q3);

                    System.out.println("a: " +a3);
                    System.out.println("b: " +b3);
                    System.out.println("c: " +c3);
                    System.out.println("d: " +d3);

                    answer = keyboard.next().charAt(0);

                    if(answer == answer3)
                    {
                        System.out.println("That was correct");
                        score ++;
                    }
                    else
                    {
                        System.out.println("That was incorrect");
                    }



                    System.out.println(q4);

                    System.out.println("a: " +a4);
                    System.out.println("b: " +b4);
                    System.out.println("c: " +c4);
                    System.out.println("d: " +d4);

                    answer = keyboard.next().charAt(0);

                    if(answer == answer4)
                    {
                        System.out.println("That was correct");
                        score ++;
                    }
                    else
                    {
                        System.out.println("That was incorrect");
                    }

                    System.out.println("You achieved a score of " +score +" out of 4");
                    System.out.println("Would you like to play again?  y/n ");
                    char yn = keyboard.next().charAt(0);

                    if(yn == 'y')
                    {
                        notStop = true;
                    }
                    else
                    {
                        notStop = false;
                    }
                }
                System.out.println("Would you like to make a new quiz? y/n");
                char yn = keyboard.next().charAt(0);
                if(yn == 'y')
                {
                    newQuiz = true;
                }
                else
                {
                    newQuiz = false;
                }

            }while(newQuiz = true);
        }
    }
}


public static void gettingQuestion()
{
    System.out.println("Please enter the question");
    question = keyboard.nextLine();
}//getting the questions

public static void gettingAnswers()
{
    System.out.println("Please enter in the four answers each on its own line.");
    ans1 = keyboard.nextLine();
    ans2 = keyboard.nextLine();
    ans3 = keyboard.nextLine();
    ans4 = keyboard.nextLine();

}

public static void gettingCorrectAnswer()
{
    System.out.println("Please enter the correct answer. a/b/c/d");
    correctAnswer = keyboard.next().charAt(0);
}

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

}

}

结果是这样的:

Please enter the question
ahsdf
Please enter in the four answers each on its own line.
adsfh
adsfh
asdfh
asdfh
Please enter the correct answer. a/b/c/d
a
Please enter the question
Please enter in the four answers each on its own line.
asdf
asdfh
asdfh
asdfh
Please enter the correct answer. a/b/c/d
a
Please enter the question
Please enter in the four answers each on its own line.
asdfh
asdfh
asdfh
adsfh
Please enter the correct answer. a/b/c/d
asdfh
Please enter the question
Please enter the correct answer. a/b/c/d
asdfh
null
a: null
b: null
c: null
d: null
a
That was correct
null
a: null
b: null
c: null
d: null
a
That was correct
null
a: null
b: null
c: null
d: null
a
That was correct
null
a: null
b: null
c: null
d: null
a
That was correct
You achieved a score of 4 out of 4
Would you like to play again?  y/n 
y
Would you like to make a new quiz? y/n
n
Please enter the question
Please enter in the four answers each on its own line.

等等

4

4 回答 4

2

我已经粘贴了您的一些代码来解释正在发生的事情。我们从这里开始:

public static void mainMenu()
{
    {
        {
            do{
                gettingQuestion(); 
                question = q1;

在下面的行中,您正在从键盘读取并加载ans1、ans2、ans3 和 ans4变量中的所有值。

                gettingAnswers();

一切正常,但是当您执行以下操作时:

                ans1 = a1;
                ans2 = b1;
                ans3 = c1;
                ans4 = d1;

您正在覆盖ans1、ans2、ans3 和 ans4 值,这就是您有空值的原因

作为一个额外的说明,您可以处理数组对象以使您的代码具有更多的顺序,它可能是这样的

public class Question{

private String realAnswer
private String question;
private String[] fakeAnswers = new String[4];

public Question(String realAnswer, String question, String [] fakeAnswers){
    this.realAnswer = realAnswer;
    this.question = question;
    this.fakeAnswers = fakeAnswers;
}

/*
   Some getters and setters
*/

}

然后您可以创建一个包含以下内容的类:

public static void main(String[] args){

// Number 10 is the quantity of questions that your quiz will have
Question[] question = new Question[10];

/*
More code
*/

}
于 2013-08-14T15:33:32.577 回答
2

经典错误:使用==toStrings而不是equals. 这不是错误;是你和你的代码。

于 2013-08-14T14:59:29.767 回答
1

你分配nullquestion这里。

gettingQuestion();
question = q1;    // q1 is null.

// 这里 question 和 q1 都为 null,因此它在答案中打印 null。

因此输出打印为空。

还要更改while condition.

于 2013-08-14T15:00:57.750 回答
1

next() 处理行尾。因此,当您nextLine()再次调用时,它会将您之前输入的enter ( \n) 作为输入。因此,它“跳过”了实际输入,并吞下\nnext(). 你有两个解决方案:

  • nextLine()在真实之前调用另一个nextLine()(所以它会吞下\n)。
  • 摆脱next并替换为nextLine().

另一件事:

赋值的表达式返回赋值的值,看这个:

while(newQuiz = true);

while循环之外的东西总是 true.

另外请注意,写是多余的,写if(variable == true)就够了if(variable)

于 2013-08-14T15:07:42.350 回答