首页 > 解决方案 > 当用户答错问题时,我该如何结束这个游戏?

问题描述

我正在为我的计算机编程课程做作业,我选择做一个琐事游戏。这场比赛是关于羽毛球的。我已经制作了游戏,但我不知道如何结束它。

例如,当用户答错一个问题时,游戏应该结束。但在我的代码中,它会打印出下一个问题。

关于我可以添加哪些行或需要进行哪些更改的任何想法?谢谢

public class Trivia {

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

        System.out.println("Here are the game rules: ");
        System.out.println("1. You do not have any power ups to use");
        System.out.println("2. You may only get one question wrong");
        System.out.println("3.If you get any question wrong, you will get what you win, and the game will end");
        System.out.println("Try your best to answer the questions");
        System.out.println("Type Start to begin");

        String start = input.next();
        String word = null;

        if (start.equalsIgnoreCase("Start")) {
            Questions(word);
        }
    }
    public static void Questions(String word) {
        Scanner input = new Scanner (System.in);
        int total;
        int a = 500;
        int b = 850;
        int c = 1000;
        int d = 1250;
        int e = 1850;
        int f = 2000;
        int g = 2500;
        int h = 3000;

        System.out.println("1. What is the maximum number of players that can play Badminton in one court?  Worth $500");
        String [] o = {"A: 1", "B: 3", "C: 4"};
        System.out.printf("%4s %10s %10s\n", o);

        String q1 = input.next();

        if (q1.equalsIgnoreCase("C")) {
            System.out.println("Your answer is correct!  You won $500");
        } else {
            System.out.println("Game over! Try again next time");
        }

        System.out.println();
        System.out.println("2. In doubles play, the first serve for the serving side begins in what court?  Worth $850");
        String [] q2 = {"A: Right service of court", "B: Left service of court", "C: Either side"};
        System.out.printf("%4s %30s %20s\n", q2);

        String ans = input.next();

        if (ans.equalsIgnoreCase("A")) {
            System.out.println("Your answer is correct!  You won $850");
            total = a + b;
            System.out.println("Your total is: $" +total);
        }
        else {
            System.out.println("Game over! You won $500");
        }

        System.out.println();
        System.out.println("3. The game of badminton became popular in the United States during what time?  Worth $1000");
        String [] q3 = {"A: 1930", "B: 1830", "C: 1500"};
        System.out.printf("%4s %30s %20s\n", q3);

        String ans3 = input.next();

        if (ans3.equalsIgnoreCase("B")) {
            System.out.println("Your answer is correct!  You won $1000");
            total = a + b + c;
            System.out.println("Your total is: $" +total);
        } else {
            System.out.println("Game over! You won $1350");
        }

        System.out.println();
        System.out.println("4. During the game, may you hit the shuttle two times in a row?  Woth $1250");
        String [] q4 = {"A: Yes", "B: No", "C: Both A and B"};
        System.out.printf("%4s %10s %18s\n", q4);

        String ans4 = input.next();

        if (ans4.equalsIgnoreCase("B")) {
            System.out.println("Your answer is correct!  You won $1250");
            total = a + b + c + d;
            System.out.println("Your total is: $" +total);
        } else {
            System.out.println("Game over! You won $2350");
        }

标签: java

解决方案


只需将 return 语句放在每个 else 条件中。

else{
      System.out.println("Game over! Try again next time");
      return;
}

希望有帮助..!!


推荐阅读