有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何实现一个数组来存储骰子/根据掷骰子的数量移除它们?

我得到了一个我基本能理解的家庭作业问题。但是,我的5个骰子需要一个数组。我不知道如何设置阵列,因为我可以掷骰子,如果他们落在2或5上,他们将从骰子中移除。目前唯一能让我的while循环工作的是我的得分系统。当我掷骰子/骰子时,我需要将数组放在那里。无论如何,我想我已经完成了大部分工作,除了存储骰子的数组。我对编程非常陌生,希望将来能理解这一点

以下是我需要我的程序完成的简要总结:

  1. 制作五个骰子的数组

  2. 掷5个骰子。(6面)

  3. a.检查是否存在2
    b、 检查是否存在2或5

  4. 如果有2或5,总和为0,可用骰子数减少2或5

  5. 如果没有2或5,求和骰子并将其加到总数中

  6. 当有骰子要掷时,继续掷直到你的骰子用完

我已经编写了大部分代码,满足了游戏的要求。但是,我被困在阵列上,不知道如何实现它

import java.util.Scanner;

public class StuckInTheMudd {

    static int rollDice() {

        int die1 = (int) (6 * Math.random()) + 1;
        return die1;
    }

    public static void main(String[] args) {
        int die1;
        int die2;
        int die3;
        int die4;
        int die5;

        int playerPoints = 0;
        int roundPoints = 0;
        String answer;
        Scanner input = new Scanner(System.in);

        System.out.println("Hello, Welcome To Stuck In The Mud");

        while (playerPoints <= 100) {
            System.out.println("Please roll the dice by pressing 'y' and hitting enter");
            System.out.println("o=================o");

            answer = input.next();

            if (answer.equalsIgnoreCase("y")) {
                die1 = rollDice();
                die2 = rollDice();
                die3 = rollDice();
                die4 = rollDice();
                die5 = rollDice();

                System.out.println("Dice 1: " + die1);
                System.out.println("Dice 2: " + die2);
                System.out.println("Dice 3: " + die3);
                System.out.println("Dice 4: " + die4);
                System.out.println("Dice 5: " + die5);

                if (die1 == 2 || die2 == 2 || die3 == 2 || die4 == 2 || die5 == 2) {
                    roundPoints = 0;
                    System.out.println("The Player loses the points for this round.");
                    System.out.println("o=================o");
                    System.out.println("Players points: " + playerPoints);
                    System.out.println("o=================o");
                } else if (die1 == 5 || die2 == 5 || die3 == 5 || die4 == 5 || die5 == 5) {
                    roundPoints = 0;
                    System.out.println("The Player loses the points for this round.");
                    System.out.println("o=================o");
                    System.out.println("Players points: " + playerPoints);
                    System.out.println("o=================o");
                } else {
                    roundPoints = roundPoints + die1 + die2 + die3 + die4 + die5;
                    playerPoints = playerPoints + roundPoints;
                    System.out.println("Current Round Points: " + roundPoints);
                    System.out.println("o=================o");}

            } else {
                System.out.println("Invalid entry, please try again.");
            }

        }
        System.out.println("Game Over and end of loop");
    }
}

再一次,我需要一个存储骰子的数组。如果骰子落在2秒或5秒上,它们将从阵列中移除。这个循环将继续,直到我没有更多的骰子了,游戏结束


共 (3) 个答案

  1. # 1 楼答案

    我会为骰子项目创建一个int数组,但我不会从中删除项目(如果您想这样做,List是一个更好的选择)

    int[] dices = new int[5];
    

    我会使用一个简单的常量值将一个特定的骰子标记为已移除,-1适用于此

    private final static int NO_DICE = -1;
    
    
    public static void main(String[] args) {
        int[] dices = new int[5];
    
        int playerPoints = 0;
        int roundPoints = 0;
        String answer;
        Scanner input = new Scanner(System.in);
    
        System.out.println("Hello, Welcome To Stuck In The Mud");
    
        while (playerPoints <= 100) {
            System.out.println("Please roll the dice by pressing 'y' and hitting enter");
            System.out.println("o=================o");
    
            answer = input.next();
    
            if (answer.equalsIgnoreCase("y")) {
                for (int i = 0; i < dices.length; i++) {
                    if (dices[i] == NO_DICE) {
                        continue;
                    }
                    dices[i] = rollDice();
                    System.out.println("Dice " + i + ": " + dices[i]);
                }
    

    然后在检查2或5的时候

    for (int i = 0; i < dices.length; i++) {
        if (dices[i] == NO_DICE) {
            continue;
        }
        if (dices[i] == 2 || dices[i] == 5) {
             dices[i] = NO_DICE;
             roundPoints = 0;
             //...
         }
    }
    
  2. # 2 楼答案

    您可以只使用位掩码而不是数组:

    int bitmask = 0b11111; // 5 bits = 5 dice
    

    要读取有多少个骰子,只需使用Integer.bitCount()

    int dicesLeft = Integer.bitCount(bitmask);
    

    要卸下模具,可以使用位移动:

    bitmask >>= n;
    

    其中n是要移除的骰子数量

  3. # 3 楼答案

    不幸的是,您无法从数组本身删除,因为创建时大小是固定的(因此,如果您可以使用某种类型的List,这将使它更容易)。因此,您必须创建一个新数组,而不包含您不需要的元素

    也许可以遍历现有数组,并在新数组中为每个数组设置值,除非它是2或5。下面是一个简单的例子:

    static int[] removeElementFromArray(int[] arr) {
        int[] newArr = new int[arr.length - 1];
        int temp = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != 5 && arr[i] != 2) {
                newArr[temp] = arr[i];
                temp++;
            }
        }
        return newArr;
    }
    

    这里有一个online示例,但显然是存储返回的数组而不是打印它