如何解决这个问答游戏的循环问题?

carvr3hs  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(182)

我在做一个学校项目的问答游戏,我有一些循环的问题。它只有5个问题,因为我只是测试如何使这个工作。回答完第五个问题后,它又回到第二个问题,让我再次回答问题。循环三次,然后继续显示结果屏幕。有没有办法解决这个问题,或者我应该用if-else而不是switch?我试过使用(questions[i]),但它只是显示了第一个问题

public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);

    int score = 0;
    int correct = 5;

    System.out.println("QUIZ GAME");
    System.out.println("5 points per correct answer. Reach 20 to pass.");

    String questions[] = {"\nWhat is the age of the earth?", "How did Hitler die?", "What year did World War II Start?", "Who invented the airplane?", "What are the names of the Atomic Bomb the US dropped on Japan?"};
    System.out.println("There are five questions.");
    for(int i = 0; i < 4;i++)
    {
        switch(i)
        {
            case 0: 
            {
                System.out.println(questions[0]);
                System.out.println("1. 9000 Years");
                System.out.println("2. 2400 Years");
                System.out.println("3. 4.2 Billion Years");
                System.out.println("4. 500 Million Years.");
                int choice1 = sc.nextInt();

                if (choice1 == 1)
                {
                    System.out.println("Wrong.");
                    System.out.println("Score = " + score);
                }
                else if (choice1 == 2)
                {
                    System.out.println("Wrong.");
                    System.out.println("Score = " + score);
                }
                else if (choice1 == 3)
                {
                    System.out.println("Right!");
                    score = addition(score, correct);
                    System.out.println("Score = " + score);
                }
                else if (choice1 == 4)
                {
                    System.out.println("Wrong.");
                    System.out.println("Score = " + score);
                }

            }
                case 1:
            {
                System.out.println("\n\n" + questions[1]);
                System.out.println("1. Died of old age.");
                System.out.println("2. Executed on 4th of May, 1945 by guillotine on Paris, France.");
                System.out.println("3. Killed on action.");
                System.out.println("4. Suicide.");
                int choice2 = sc.nextInt();
                if (choice2 == 2)
                {
                    System.out.println("Wrong.");
                    System.out.println("Score = " + score);
                }
                else if (choice2 == 2)
                {
                    System.out.println("Wrong.");
                    System.out.println("Score = " + score);
                }
                else if (choice2 == 3)
                {
                    System.out.println("Wrong.");
                    System.out.println("Score = " + score);
                }
                else if (choice2 == 4)
                {
                    System.out.println("Right!");
                    score = addition(score, correct);
                    System.out.println("Score = " + score);
                }
            }
            case 2:
            {
                System.out.println("\n\n" + questions[2]);
                System.out.println("1. 1939");
                System.out.println("2. 1935");
                System.out.println("3. 1931");
                System.out.println("4. 1942");
                int choice3 = sc.nextInt();
                if (choice3 == 1)
                {
                    System.out.println("Right!");
                    score = addition(score, correct);
                    System.out.println("Score = " + score);
                }
                else if (choice3 == 2)
                {
                    System.out.println("Wrong.");
                    System.out.println("Score = " + score);
                }
                else if (choice3 == 3)
                {
                    System.out.println("Wrong.");
                    System.out.println("Score = " + score);
                }
                else if (choice3 == 4)
                {
                    System.out.println("Wrong.");
                    System.out.println("Score = " + score);
                }
            }
            case 3:
            {
                System.out.println("\n\n" + questions[3]);
                System.out.println("1. Thomas Edison");
                System.out.println("2. Wilbur and Orville Wright");
                System.out.println("3. Marie and Pierre Curie");
                System.out.println("4. Albert Einstein");
                int choice4 = sc.nextInt();
                if (choice4 == 1)
                {
                    System.out.println("Right!");
                    score = addition(score, correct);
                    System.out.println("Score = " + score);
                }
                else if (choice4 == 2)
                {
                    System.out.println("Right!");
                    score = addition(score, correct);
                    System.out.println("Score = " + score);
                }
                else if (choice4 == 3)
                {
                    System.out.println("Wrong.");
                    System.out.println("Score = " + score);
                }
                else if (choice4 == 4)
                {
                    System.out.println("Wrong.");
                    System.out.println("Score = " + score);
                }
            }
            case 4:
            {
                System.out.println("\n\n" + questions[4]);
                System.out.println("1. Skinny Woman and Little Girl");
                System.out.println("2. Yeet and Yikes");
                System.out.println("3. Fat Man and Little Boy");
                System.out.println("4. The Big One and The Small One");
                int choice5 = sc.nextInt();
                if (choice5 == 1)
                {
                    System.out.println("Wrong.");
                    System.out.println("Score = " + score);
                }
                else if (choice5 == 2)
                {
                    System.out.println("Right!");
                    score = addition(score, correct);
                    System.out.println("Score = " + score);
                }
                else if (choice5 == 3)
                {
                    System.out.println("Right!");
                    score = addition(score, correct);
                    System.out.println("Score = " + score);
                }
                else if (choice5 == 4)
                {
                    System.out.println("Wrong.");
                    System.out.println("Score = " + score);
                }

            }
            int fin = 1;

            if (score >= 20)
            {
                System.out.println("You passed! Congratulations!");
            }
            else if (score > 20)
            {
                while (fin != 0)
                {
                    System.out.println("You failed! Do you want to do it again? [y/n]");
                    fin = sc.nextInt();
                }

            }

        }
    }

}

public static int addition(int x, int y)
{
    int total = x + y;
    return total;
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题