0

我正在编写一个掷骰子的程序。我对 java 还是很陌生,因为我正在上课。我在这个程序的不同包中使用多个类,我想弄清楚的是,在一个类中,对于我的包 pairOfDice,我在类 pairOfDice、die1 和 die2 中创建了对象。现在我有另一个包 rollDice,我的目标是使用 pairOfDice 类来掷两个骰子并显示掷骰子。我正在努力解决的是如何做到这一点。当我掷骰子时,我的结果显示就好像我只掷了一个骰子。我已经进行了调整,以每卷显示两个骰子,尽管感觉好像我没有以更熟练的方式进行操作。

package die;

import java.util.Scanner;

/**
 *
 * @author <a href= "mailto:adavi125@my.chemeketa.edu" >Aaron Davis</a>
 */
public class RollDice
{
    public static void main(String[] args)
    {


        Scanner scan = new Scanner(System.in);

        PairOfDice dice = new PairOfDice();

        // get amount of rolls from user
        System.out.print("How many rolls do you want? ");

        int numRolls = scan.nextInt();



        int diceOne, diceTwo;
        int boxCar, snakeEyes;
        int j = 0, k = 0;

        // rolls the dice the requested amount of times
        for (int i = 0; i < numRolls; i++)
        {
            // first die to roll
            diceOne = dice.roll();

            // second die to roll
            diceTwo = dice.roll();

            // display rolled dice
            System.out.println(diceOne + " " + diceTwo + "\n");

            // store and display pairs of 1 rolls
            if (diceOne == 1 && diceTwo == 1)
            {
                snakeEyes = ++j;

                System.out.println("\nThe number of snake eyes you have is: " 
                    + snakeEyes + "\n");
            }


            // store and display pairs of 6 rolls
            if (diceOne == 6 && diceTwo == 6)
            {
                boxCar = ++k;

                System.out.println("\nThe number of box cars you have is: " 
                    + boxCar + "\n");
            }



        }




    }    
}


******************************************************************************
/*
 the integers diceOne and diceTwo are my workarounds, my other package contains

public class PairOfDice extends Die
{
    Die die1, die2;

    public PairOfDice()
    {
        die1 = new Die();
        die2 = new Die();     
    }

    public PairOfDice(int face)
    {
        die1 = new Die(face);
        die2 = new Die(face);
    }
}

*/
******************************************************************************

// i am un-clear how to make "PairOfDice dice = new PairOfDice();" come out as two die
4

1 回答 1

0

该类PairOfDice不代表您的模型,即“一对骰子”。如果你有一对骰子,当你滚动它们时,你会得到两个不同的数字,因此:

  1. 您分别对这两个值感兴趣,因此roll方法必须返回两个值。例如,您可以使用RollResult包含这两个值的 bean
  2. 您对这两个值都不感兴趣,而对总和感兴趣。这样,该roll方法可以只返回一个从 2 到 12 的整数,您可以根据它们的总和推测骰子滚动:在您的情况下,它总是有可能的,因为当且仅当您的骰子为 1 时,您得到的总和为 2 , 1; 类似地,如果当且仅当您的骰子为 6、6 时,总和为 12。例如,如果您针对条件“骰子 1=3,骰子 2=4”进行测试,则它不起作用,因为有很多返回 3+4=7 的轧制组合

希望这可以帮助。

根据评论,我们必须继续第一个解决方案。这是一个实现域不可变对象和roll域函数的示例,该函数返回roll针对骰子的操作结果。在示例中,我展示了拥有多种类型骰子的可能性。

import java.util.*;
import java.util.stream.Collectors;

public class RollingDices {
    private static final Random RND = new Random();
    private static interface Dice {
        public int roll();
    }
    private static class UniformDice implements Dice {
        public int roll() {
            return RND.nextInt(6) + 1;
        }
    }
    private static class TrickyDice implements Dice {
        private final int value;
        public TrickyDice(int value) {
            this.value = value;
        }
        public int roll() {
            return value;
        }
    }
    private static class ProbabilityTableDice implements Dice {
        private final Double[] probabilities;
        public ProbabilityTableDice(Double ... probabilities) {
            if (Arrays.stream(probabilities).mapToDouble(Double::doubleValue).sum() != 1.0) {
                throw new RuntimeException();
            }
            this.probabilities = probabilities;
        }

        public int roll() {
            final double randomValue = RND.nextDouble();
            double curValue = 0.0;
            for (int i = 0; i < this.probabilities.length; i++) {
                curValue += this.probabilities[i];
                if (curValue >= randomValue) {
                    return i + 1;
                }
            }
            throw new RuntimeException();
        }
    }
    private static class CollectionOfDices {
        private final Dice[] dices;

        public CollectionOfDices(Dice ... dices) {
            this.dices = dices;
        }

        public List<Integer> roll() {
            return Arrays.stream(dices).map(Dice::roll).collect(Collectors.toList());
        }
    }
    private static class DicesFactory {
        private static final DicesFactory INSTANCE = new DicesFactory();

        public static DicesFactory instance() {
            return INSTANCE;
        }

        private DicesFactory() {}

        private final Dice uniformDice = new UniformDice();
        public Dice createUniformDice() {
            return this.uniformDice;
        }
        public Dice createTrickyDice(int fixedValue) {
            return new TrickyDice(fixedValue);
        }
        public Dice createProbabilityTableDice(Double ... probabilities) {
            return new ProbabilityTableDice(probabilities);
        }
    }

    public static void main(String ... args) {
        final Scanner scan = new Scanner(System.in);

        final CollectionOfDices dice = new CollectionOfDices(
                DicesFactory.instance().createUniformDice(),
                DicesFactory.instance().createProbabilityTableDice(
                        0.15, 0.2, 0.3, 0.1, 0.25
                )
        );

        // get amount of rolls from user
        System.out.print("How many rolls do you want? ");

        int numRolls = scan.nextInt();



        int diceOne, diceTwo;
        int boxCar, snakeEyes;
        int j = 0, k = 0;

        // rolls the dice the requested amount of times
        for (int i = 0; i < numRolls; i++)
        {
            final List<Integer> rolls = dice.roll();
            // first die to roll
            diceOne = rolls.get(0);

            // second die to roll
            diceTwo = rolls.get(1);

            // display rolled dice
            System.out.println(diceOne + " " + diceTwo + "\n");

            // store and display pairs of 1 rolls
            if (diceOne == 1 && diceTwo == 1)
            {
                snakeEyes = ++j;

                System.out.println("\nThe number of snake eyes you have is: "
                        + snakeEyes + "\n");
            }


            // store and display pairs of 6 rolls
            if (diceOne == 6 && diceTwo == 6)
            {
                boxCar = ++k;

                System.out.println("\nThe number of box cars you have is: "
                        + boxCar + "\n");
            }

        }

    }
}
于 2019-02-03T08:25:11.730 回答