java - 试用代码中的奇怪错误

标签 java

这是我一直在编写的一些代码,用于一个简单的酒吧风格测验来学习 Java。由于某种原因,它会第一次问这个问题,然后就跳过它。即使我初始化了字符串,它也会将它们声明为空。

这是代码

    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.

等等

最佳答案

我粘贴了您的一些代码来解释发生了什么。我们从这里开始:

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
*/

}

然后你可以创建一个包含如下内容的ma​​in类:

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
*/

}

关于java - 试用代码中的奇怪错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18235194/

相关文章:

java - Spring JPA 存储库不返回实体列表

java - 使用私有(private)构造函数对 spring 组件进行单元测试,无需注入(inject)

java - Java如何将 float 转换为字符串

Java 十进制格式 : how to reject "invalid" input with grouped decimals?

java - 关于tomcat的负载能力(最好选择哪个版本?)

java - Java 中的欧拉项目 215

java - 无法从 AWS EC2 发送 SMTP 邮件

java - Jav8 : Computing Elapsed time for Timestamp

JavaFX : How to dynamically add textfields and buttons on user input

java - 如何用javap查看Java代码中有哪几行字节码对应的行?