我对java很新,我为自己制定了一个目标来制作一个骰子滚动程序(保持它很小)。最终目标是能够掷出用户选定数量的骰子,并且能够让每个骰子具有不同数量的边,如果需要的话,我可以得到骰子的数量以及每个骰子的数量。这是我为它所做的代码(可能是真的不好,对不起,如果它是):显示一个多骰子滚轮的某个输出
public class Roller {
public final Random rando;
public final int faces;
public Roller(int faces) {
this.rando = new Random();
this.faces = faces;
}
public int roll() {
return 1 + rando.nextInt(faces);
}
//above code is not mine I built off what my friend wrote cause i didnt know if i still need it
public static void main(String[] args) {
Random rand = new Random();
Scanner scan = new Scanner(System.in);
System.out.print("How many dice do you want to roll?\n");
int D6 = scan.nextInt();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < D6; i++) {
System.out.print("How many sides does die " + (i + 1) + " have?\n");
Roller dice = new Roller(scan.nextInt());
list.add(dice.roll());
}
}
}
现在我在这里我想显示ArrayList
点,但我想它显示为
“骰子1卷#
骰子2卷#”
等等,我失去了对如何做到这一点尤其是与不同数量的骰子。任何帮助非常感谢。
我建议你使用另一个for循环类似于你已经有的循环。 –
我还建议您了解Java命名约定。类通常以大写字母开头,变量名以小写字母开头。接下来使用更多的描述性名称。例如'D6'应该是'diceCount'。 –
如果D6是对D&D的引用,它可能比'diceCount'更具描述性,但这是非常情景化和基于上下文的。将它用作扫描仪输入的变量名称表明这可能不是这种情况。 – SaxyPandaBear