我的龙与地下城掷骰机有问题吗 import java.util.Scanner; 公共类项目2主要{ 公共静态void main(字符串[]args){ 扫描仪kb=新扫描仪(System.in); int numberOfSets=0; System.out.println(“您想要掷多少组骰子?”); numberOfSets=kb.nextInt(); kb.nextLine(); 对于(int i=0;i

我的龙与地下城掷骰机有问题吗 import java.util.Scanner; 公共类项目2主要{ 公共静态void main(字符串[]args){ 扫描仪kb=新扫描仪(System.in); int numberOfSets=0; System.out.println(“您想要掷多少组骰子?”); numberOfSets=kb.nextInt(); kb.nextLine(); 对于(int i=0;i,java,dice,Java,Dice,这是我的主要课程。整个课程的重点是在六面模具上取四个不同的辊,检查哪一个辊最低,然后将三个最高的辊加在一起,同时告诉用户它们的最低辊是什么 我遇到的主要问题是上骰子课 我知道我需要一个int来保存骰子值,一个构造函数来实际创建随机整数,一个getter来实际将随机整数返回到主类。我该怎么做呢 另一个问题:我如何让用户选择一组并重新滚动该组中的最低值?我的意思是,当用户滚动时,三个最大的骰子滚动被加在一起,而最低的一个被推到一边。用户必须被迫重新滚动三个最高骰子滚动中的最低值,即使他们不想这样做。

这是我的主要课程。整个课程的重点是在六面模具上取四个不同的辊,检查哪一个辊最低,然后将三个最高的辊加在一起,同时告诉用户它们的最低辊是什么

我遇到的主要问题是上骰子课

我知道我需要一个int来保存骰子值,一个构造函数来实际创建随机整数,一个getter来实际将随机整数返回到主类。我该怎么做呢

另一个问题:我如何让用户选择一组并重新滚动该组中的最低值?我的意思是,当用户滚动时,三个最大的骰子滚动被加在一起,而最低的一个被推到一边。用户必须被迫重新滚动三个最高骰子滚动中的最低值,即使他们不想这样做。

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


如果这没有多大意义,我很抱歉,因此如果有人想建议编辑以使其更清楚,那就太好了。

自定义骰子滚动类,它可以滚动任意数量的4d6下降最低集

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();
}
import java.util.*;
公营掷骰机
{
私有随机兰德;
公众掷骰子()
{
this.rand=new Random();
}
公共int-rollSingleDie()
{
返回兰特nextInt(6)+1;
}
公共列表ROLL4D6DROPOWEST()
{
List retList=new ArrayList();
//向列表中添加4个数字以模拟4个转鼓。

对于(int i=0;我想你已经试过了吗?我想你应该使用一个可能有不同值的数组和一个返回随机数的getValue。我已经知道我要用我的dice类做什么。我希望它有一个构造函数和一个getter在dice类中掷骰子,然后让它能够将这些值返回给ma在类中。如果可能的话,我也想避免使用数组。使用
Roll()
方法和构造函数的
Dice
类如何?该类可以使用最大可能滚动值(6表示6面)?此外,您当前的代码不会输出任何内容,因为所有的骰子都是0,并且0从不小于0——您不会滚动“骰子”(ints)无论如何,仅供参考,它是“伪代码”而不是“sudo代码”。我通常会在编辑中更正这种类型的内容,但实际上整个第一段与实际问题无关,因此如果有任何问题,我会将整个内容编辑掉。很高兴我能提供帮助。别忘了投票和/或接受解决问题的答案。你误解了我在上一篇文章中提出了我的问题,但我想我会像你问的那样做。我要问的是A)我如何强制用户在他们选择的集合中重新掷三个最高数字中最低的一个。例如:用户掷6、5、3。这三个数字被完全忽略,并且5被打印为三个最高数字中最低的一个。现在假设还有四组其他骰子,这组是数字1。我希望用户能够键入1和h请将那个较低的数字(5)重新滚动到另一个数字。这有意义吗?@Polansi现在有意义了。我已经相应地更新了我的答案。大多数更改都在您的主方法中,尽管我还在
Dicerroll
类中添加了一个
rollSingleDie
方法。如果您还有其他问题,请告诉我。
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;
}