我的龙与地下城骰子滚筒有问题

Having trouble with my dungeons and dragons dice roller

import java.util.Scanner;

public class Project2Main {

public static void main(String[] args) {

    Scanner kb = new Scanner(System.in);
    int numberOfSets = 0;
    System.out.println("How many sets of dice would you like to roll?");
    numberOfSets = kb.nextInt();
    kb.nextLine();

    for (int i = 0; i < numberOfSets; i++) {
        int dice1 = 0;
        int dice2 = 0;
        int dice3 = 0;
        int dice4 = 0;
        int diceTotal = 0;

        if (dice1 < dice2 && dice1 < dice3 && dice1 < dice4) {
            diceTotal = dice2 + dice3 + dice4;
            System.out.println("Your roll total is " + diceTotal + " and your lowest roll was " + dice1 + ".");
            break;
        } else if (dice2 < dice1 && dice2 < dice3 && dice2 < dice4) {
            diceTotal = dice1 + dice3 + dice4;
            System.out.println("Your roll total is " + diceTotal + " and your lowest roll was " + dice2 + ".");
            break;
        } else if (dice3 < dice1 && dice3 < dice2 && dice3 < dice4) {
            diceTotal = dice1 + dice2 + dice4;
            System.out.println("Your roll total is " + diceTotal + " and your lowest roll was " + dice3 + ".");
            break;
        } else if (dice4 < dice1 && dice4 < dice2 && dice4 < dice3) {
            diceTotal = dice1 + dice2 + dice3;
            System.out.println("Your roll total is " + diceTotal + " and your lowest roll was " + dice4 + ".");
            break;
        }
    }
    kb.close();
}

这是我的主class。这样做的重点是取一个六面骰子的四个不同的掷骰子,检查哪个掷骰子最低,然后将最高的三个掷骰子加在一起,同时告诉用户他们的最低掷骰子是多少。

我遇到的主要问题是掷骰子 class。

我知道我需要一个 int 来保存骰子值,一个构造函数来实际创建随机整数,以及一个 getter 来实际 return 主要的随机整数 class.我该怎么做呢?

另一个问题:如何让用户选择一个集合并重新掷出该集合中的最低值?我的意思是当用户掷骰子时,三个最大的骰子卷加在一起,最小的一个放在一边。即使他们不想,也必须强制用户重新掷三个最高骰子中的最低值。

用户必须能够输入一个数字来指示最终将重新滚动哪一组。

如果这没有多大意义,我深表歉意,所以如果有人想建议修改以使其更清楚,那就太好了。

自定义 DiceRoller class 能够滚动任意数量的 4d6-drop-lowest 组。

import java.util.*;

public class DiceRoller
{
    private Random rand;

    public DiceRoller()
    {
        this.rand = new Random();
    }

    public int rollSingleDie()
    {
        return rand.nextInt(6)+1;
    }

    public List<Integer> roll4d6DropLowest()
    {
        List<Integer> retList = new ArrayList<Integer>();

        // Add 4 numbers to the list to simulate 4 rolls.
        for (int i=0; i<4; i++)
        {
            retList.add(rollSingleDie());
        }

        // Remove the lowest roll of the 4. 
        retList.remove(Collections.min(retList));

        return retList;
    }

    public List<List<Integer>> rollSets(int numSets)
    {
        List<List<Integer>> results = new List<List<Integer>>();
        for (int i=0; i<numSets; i++)
        {
            results.add(roll4d6DropLowest());
        }
        return results;
    }
}

并且在您的主要方法中:

public static void main(String args[])
{
    /*** Rolling the initial sets ***/
    int numSets = 10; // Ten is a placeholder.
                      // This is where you get input from user.

    DiceRoller roller = new DiceRoller();
    List<List<Integer>> diceSets = roller.rollSets(numSets);

    for (List<Integer> diceRolls : diceSets)
    {
        Integer total = sum(diceRolls);
        Integer lowest = Collections.min(diceRolls);
        System.out.println("Total is : " + total + " and lowest is : " + lowest);
    }

    /*** Forcing user to reroll lowest of one set ***/
    int setToRerollIndex = 1; // Placeholder value.
    // Replace with input from user on which set to reroll.
    // Remember to adjust user input - they will probably use a number between 1 
    // and the number of sets, but the List needs an index between 0 and the number of
    // sets - 1. 
    List<Integer> setToReroll = diceSets.get(setToRerollIndex);
    int indexOfLowest = setToReroll.indexOf(Collections.min(setToReroll));
    setToReroll.set(indexOfLowest, roller.rollSingleDie());

    // Selected set has now rerolled the lowest value.
}

public static int sum(List<Integer> list)
{
    int sum= 0; 
    for (int number : list)
        sum = sum + number;
    return sum;
}