java - java中的骰子总和应用程序。我得到了索引错误,我设计了错误的代码,但我不知道在哪里
问题描述
package HomePlace;
import java.util.Scanner;
import java.util.Random;
public class DiceRolls {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many sides? ");
int Usides = input.nextInt();
System.out.print("How many dice to roll? ");
int Udices = input.nextInt();
System.out.print("How many rolls? ");
int Urolls = input.nextInt();
int[] outcomes = new int[Usides * Udices];
for (int rolls = 0; rolls < Urolls; rolls++) {
outcomes[sum(Usides, Udices)] += 1;
}
for (int i = Udices; i <= Urolls; i++) {
System.out.println(i + ": " + outcomes[i-1]);
}
}
public static int sum(int Us, int Ud) {
int dicesum = 0;
for (int i = 0; i <= Ud; i++) {
int dots = (int) (Math.random() * Us + 1);
dicesum += dots;
}
return dicesum;
}
}
这是一个代码,用户声明(骰子的)有多少面,有多少骰子(骰子的数量),以及用户将执行多少次掷骰。例如,如果用户选择骰子有 3 个面,则变量将为 1~3,如果用户输入 5 个骰子和 5 次掷骰,则将掷 5 个(3 面)骰子 5 次。我的输出应该显示可能的总和以及该总和出现的次数。例如,如果我掷 2 次 6 面骰子 2 次,则输出应计算每次掷出的总和数。
现在我的问题是我得到了错误
How many sides? 3
How many dice to roll? 4
How many rolls? 5
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 12 out of bounds for length 12
at HomePlace.DiceRolls.main(DiceRolls.java:21)
如果我投入大量资金
How many sides? 123
How many dice to roll? 123
How many rolls? 123
123: 0
我只得到这个输出。我希望输出在哪里
How many sides on the die? 7
How many dice to roll? 5
How many rolls? 100
5: 0
6: 0
7: 0
8: 0
9: 1
10: 1
11: 2
12: 0
13: 2
14: 5
15: 9
16: 5
17: 4
18: 2
19: 9
20: 6
21: 8
22: 11
23: 10
24: 7
25: 6
26: 5
27: 2
28: 1
29: 3
30: 1
31: 0
32: 0
33: 0
34: 0
35: 0
显然我在那段代码中做错了..我能得到帮助吗?
解决方案
以您问题中的第一个样本为例,即四个骰子,每个骰子有三个面,您总共掷出所有四个骰子五次。
当每个骰子落在 1(一)上时,最小结果将是 4,即最小结果等于骰子的总数。
当每个骰子落在 3(三)上时,最大结果将是 12,即最大结果是骰子数乘以每个骰子的面数。
数组outcomes
存储每个结果的频率。所以数组的大小需要是可能结果的数量。使用上面的示例,大小需要为 9,即 12 - 4 + 1,即最大结果减去最小结果加一。
方法中的第一个for
循环main()
执行掷骰子并调整数组中的值outcomes
。您需要将实际结果调整为数组索引。再次使用上面的示例,如果结果是最小值,即 4,则对应于 中的第一个元素outcomes
,因此您需要将 4 更改为 0(零)。同样,当结果为 5 时,您要更新 中的第二个元素outcomes
,因此需要将 5 更改为 1(一)。如您所见,在这两种情况下,您都需要从获得的实际结果中减去最小结果,以获得outcomes
需要递增的元素的实际索引。
方法中的for
循环sum()
需要根据骰子的数量重复。所以对于四个骰子,循环需要重复四次。所以你可以把循环写成:
for (int i = 1; i <= Ud; i++) {
最后打印结果。您想显示其中的每个元素,outcomes
但是当您显示元素值时,您需要将最小结果添加到元素索引,因为正如我在上面所写的,数组中的元素 0(零)outcomes
实际上意味着最小结果。
这是代码。我添加了更多print()
方法,以便您可以看到代码在做什么。
import java.util.Scanner;
public class DiceRolls {
public static int sum(int Us, int Ud) {
int dicesum = 0;
for (int i = 1; i <= Ud; i++) {
int dots = (int) (Math.random() * Us + 1);
System.out.printf("dice %d landed on %d%n", i, dots);
dicesum += dots;
}
return dicesum;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many sides? ");
int Usides = input.nextInt();
System.out.print("How many dice to roll? ");
int Udices = input.nextInt();
System.out.print("How many rolls? ");
int Urolls = input.nextInt();
int maximumOutcome = Udices * Usides;
int[] outcomes = new int[maximumOutcome - Udices + 1];
System.out.printf("Outcomes for %d rolls of %d, %d-sided dice.%n", Urolls, Udices, Usides);
for (int rolls = 1; rolls <= Urolls; rolls++) {
System.out.printf("%nRoll %d:%n", rolls);
int sum = sum(Usides, Udices);
System.out.printf("Total: %d%n", sum);
outcomes[sum - Udices]++;
}
System.out.println("====================================================================");
System.out.println("Outcomes: ");
for (int i = 0; i < outcomes.length; i++) {
System.out.println((i + Udices) + ": " + outcomes[i]);
}
}
}
推荐阅读
- python - 过滤 DateTimeField Django Rest框架
- android - 如何创建具有渐变背景的圆角按钮?
- php - (php) 我在“get”方法链接中得到额外的代码
- frontend - Quill 1.3.6,将所有图像粘贴到编辑器时上传
- algorithm - 在处理复合数据类型数组时,排序算法是否表现出更大的不稳定性?
- javascript - 制作动态输入范围
- sorting - 使用 DATE_TRUNC FUNCTION
- ruby-on-rails - 出于测试目的调用外部文件的语法
- sql - 使用 SQL 的 MarkLogic 日期时间
- routing - 如果我的 IP(在本地网络中)不是唯一的,外部设备如何寻址我?