创建龙与地下城骰子滚筒

Creating a Dungeons and Dragons dice roller

我的任务是创建一个龙与地下城掷骰子程序,其中掷四个骰子,记录最高的三个数字,最低的数字放在一边。

该程序需要使用面向对象编程,这就是我运行遇到问题的地方。

此外,不得使用嵌套的 If 语句或循环。

我正在尝试弄清楚如何将每个 class 彼此分开编码。

我需要一个 class 将 运行 程序本身,一个询问使用该程序的人他们想掷多少组骰子的 class实际代表一组骰子掷骰,一个 class 代表一系列掷骰子(这显示骰子 1 掷的是什么,骰子 2 掷的是什么,等等),最后一个 class 来处理单个骰子的滚动。

我知道输出需要显示每组掷出的骰子。

输出需要如下所示:

Set 1: 11, 12, 3, 1
Set 2: 12, 3, 1, 4

等等。

另外,在尝试创建这个程序时让我失望的主要事情是我被要求在没有嵌套的 if 语句或循环的情况下制作它。

这个问题的解决方案应该可以使用两个 classes 来完成:你主要 class 和一个 Dice class。

您的主 class 应该有您的 main(){},它将包含提示用户输入和打印骰子的代码。

您的 Dice class 中可能只需要两个方法,一个是构造函数,另一个是 getter 作为骰子的值。

创建骰子时,您的骰子构造器可以将骰子对象的掷骰值设置为 1 到 6 之间的随机数(假设您使用 D6 进行 DnD 掷骰)。这将使您无需调用另一种方法来实际 "roll" 骰子。

然后您可以使用 getter 从您的 main() 获得滚动值。这个getter只需要returnroll的值

您的 Dice class 需要一个成员变量,它可以保存为新骰子生成的随机值——我建议使用 int,因为您只需要整数。

在您的 main(){} 中,您可以使用扫描仪获取用户输入的他们想要掷出的骰子组数。根据他们的输入,您可以使用 for 循环,根据用户的输入检查条件。 (例如 for(int i = 0 ; i <userInput ; i ++) )。循环将为用户想要滚动的每组骰子迭代一次。

在此循环中,您可以在循环的每次迭代中创建四个 Dice 对象。这将有效地为您提供四个 "rolls",因为每个骰子都是用随机数实例化的。

假设您为 'Dice' class 中保存骰子值的成员变量命名为 roll,您可以通过像这样的点符号访问骰子值 dice1.roll()这也可以在您的输出中使用,而不需要将值 returned 分配给另一个变量。

最后,要检查哪个roll 最低,可以使用一系列if - else 语句来检查哪个roll 最低。我建议使用如下条件

if(dice1.roll() < dice2.roll() && dice1.roll() < dice3.roll() && dice1.roll() < dice3.roll()){ //print your dice rolls. If the condition is true, we know that dice1 had the //lowest roll }

然后您可以使用 else 检查 dice2 是否是最低的,另一个 else if 检查 dice3,另一个检查 dice4。

在 sudo 代码中你的主要 class 可能看起来像这样

\main

\declare a scanner
\declare int for user input

\output: How many dice would you like to roll
\input user input var is assigned the value of user input


\for loop checking if i < userInput
   \Create new dice object dice1
   \Create new dice object dice2
   \Create new dice object dice3
   \Create new dice object dice4

   \check if dice1 is lowest
       \print out die rolls, totals, and lowest roll
   \else if check dice2 is lowest
       \print out die rolls, totals, and lowest roll
   \else if check dice3 is lowest
       \print out die rolls, totals, and lowest roll
   \else if check dice4 is lowest
       \print out die rolls, totals, and lowest roll

\end for loop this loop will iterate how ever many times the user input
    \each loop will create 4 dice objects with random D6 values
    \essentially give you 4 "rolls" for each iteration of the loop

这个主要 class 没有嵌套循环或 if 语句。

您的 Dice class 可能看起来像这样

\ Dice

\declare a private int named something like roll to hold the random value

\constructor
    \assign roll a random int value between 1 and 6


\getter
    \return the random value

这应该足以让您入门。

作为额外的奖励,如果您想让程序达到 "run" 并要求用户提供另一组骰子并再次掷骰子——一遍又一遍,直到您关闭程序——,您可以包装 main() 的主体,在您提示用户输入之前开始,使用始终为真的 while 循环。这会创建一个无限循环(和嵌套循环,因此它不适用于您的解决方案)。但是,此循环将允许您的程序 "run" 直到您终止它所在的控制台 运行 。同样,如果您想将其用于您自己的用途,它不会满足要求如果你包含 while(true){} 循环,你的具体问题。

希望以上内容对您有所帮助

Mkreegs

如果您绝对需要使用您描述的四个 类,这里有一个解决方案:

注意:避免循环的两种方法是:a) 将代码输入 n 次(如果常量迭代很少),b) 使用递归方法代替。

主要:

  • 询问用户套数
  • 使用Recursive方法重复printSet()n次

设置创建者:

  • 实例化一个 SeriesRoller
  • 对 int[] 进行排序,并按照您的描述进行格式化
  • getSet() returns 字符串

系列滚筒:

  • 实例化一个 DieRoller
  • 使用递归方法掷骰子
  • getSeries() returns int[]

DieRoller:

  • 实例化一个随机数
  • getRoll() returns 从 1 到 6 的数字