java模拟器简单的赛马游戏仅使用数组和循环

标签 java arrays loops simulator currency

我正在尝试仅使用数组和循环制作一个简单的 java 模拟器赛马游戏。我的计划快完成了。我唯一的问题是,当您输入将参加比赛的马匹数量时,即使其他马匹完成比赛,您输入的特定数字也将获胜。例如,当您输入 5 匹将参加比赛的马时,即使另一个数字先完成,“5”号也将获胜。我无法真正确定获胜者。我的程序似乎运行良好。任何意见,将不胜感激。谢谢你! 这是我的代码:

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

    int[] tracks = new int[70];

    int bet;

    System.out.println("==============");
    System.out.println("||HORSE RACE||");
    System.out.println("==============");
    System.out.println("WHO'S GONNA WIN IN THIS EPIC RACE?");
    System.out.println("ENTER HOW MANY HORSES WOULD YOU LIKE TO JOIN:"
            + "\n 2-10 HORSES are allowed to join!");
    int horses;
    do {
        horses = input.nextInt();
    } while (horses < 2 || horses > 10);
    int[] move = new int[horses];
    double[] betHorse = new double[horses];

    System.out.println("Enter how many person will bet?");
    int number = input.nextInt();
    for (int i = 1; i <= number; i++) {
        do {
            for (int j = 1; j <= horses; j++) {
                System.out.println("[" + j + "]" + " for HORSE " + j);
            }
            System.out.println("Person no." + i + ": Enter the number of horse:");
            bet = input.nextInt();
        } while (bet < 1 || bet > horses);
        for (int p = 1; p <= horses; p++) {
            if (bet == p) {
                System.out.println("Enter the amount of your bet?");
                betHorse[bet - 1] += input.nextDouble();
            }
        }
        for (int j = 1; j <= horses; j++) {
            System.out.println("Bet for HORSE " + j + ":P" + betHorse[j - 1]);
        }
    }

    System.out.println("OKAY THAT'S SETTLED");
    System.out.println("Race begins in:");
    int num3 = 3;
    for (int i = 1; i <= num3; num3--) {
        System.out.println(num3);
        Thread.sleep(1000);
    }
    do {
        Thread.sleep(100);

        int[] numbers = new int[horses];
        for (int i = 0; i < horses; i++) {
            numbers[i] = 1 + (int) (Math.random() * 6);
        }
        for (int i = 0; i < horses; i++) {
            if (numbers[i] >= 1 && numbers[i] <= 3) {
                move[i]++;
            } else if (numbers[i] == 4 && numbers[i] == 5) {
                move[i] = move[i] + 3;
            } else if (numbers[i] == 6) {
                move[i] = move[i] + 5;
            }
        }
        System.out.println("\n\n\n");
        for (int i = 1; i <= horses; i++){
            System.out.println("Horse " + i +" position:" + move[i-1]);
        }
        for (int i = 1; i <= horses; i++) {
            for (int j = 0; j < move[i - 1]; j++) {
                System.out.print("--");
            }
            System.out.println(i + "H" + move[i - 1]);

        }

    } while (move[horses-1] < tracks.length );

    for (int i = 1; i <= horses; i++) {
        if (move[i - 1] > tracks.length) {
            System.out.println("HORSE " + i + " finished the track! One who bets for HORSE " + i + " won P" + betHorse[i - 1] * 2);
        }
    }

}
}

最佳答案

while 循环中的条件:

while (move[horses-1] < tracks.length)

表示一旦最后一匹马(其索引为horses-1)完成,循环就会结束。当任何马结束时,您应该更改条件以结束循环。

每当你更新 move[i] 时,你应该测试 move[i]>=tracks.length 是否,如果是,则将一些 boolean 变量设置为 true - 结束 = true;.

然后将循环条件更改为 while (!ending)

关于java模拟器简单的赛马游戏仅使用数组和循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29278632/

相关文章:

java - 如何打开三星设置页面?

java - 有两个不确定参数的方法?

c - 返回 int array[] 或指向 int array[] 的指针返回到 C 中调用例程的函数

python - 将 ND 数组合并为单个数组和列表

javascript - 检查嵌套数组中的元素

java - sikuli 安装程序未启动

java - 如何编辑从 .jar 文件导入到 Eclipse 中的 .java 文件

php - 如果内部数组中有匹配项,则获取外部数组的索引

c - 非数字输入导致死循环

python - 如何在Python中将2列表中的单词与另一个单词字符串进行匹配而无需子字符串匹配?