使用具有来自随机生成器的值的一维数组编写程序,以打印出每个组合被一对骰子掷出的次数
Write a program using 1-D arrays with values from a random generator to print out how many times each combination was rolled by a pair of dice
作业的目标是使用并行的一维数组,但也允许使用二维数组。
我可以打印出不同的组合,例如一对骰子掷出的 1,1(也称为蛇眼)。
尝试打印出每个组合的滚动次数而不打印组合的滚动次数是很困难的。
例如:
输入您想掷一对骰子的次数:
5
你掷出:1和5共1次-我不想要的
您掷出了:4和3共1次
您滚动:1 和 5 共 2 次 - 对于重复,这就是我要打印的全部内容
您掷出:3和3共1次
您掷出:2和2共1次
我知道在增加组合数组(它包含每个组合被滚动的次数)之后立即打印出来的循环是不正确的,但我不知道如何修改它。
我认为 combo[0][0] 是 1,1 的滚动次数,combo[0][1] 是 1,2 的滚动次数,依此类推。
import java.util.Scanner;
public class Dice {
Scanner read = new Scanner(System.in);
Random diceRoll = new Random();
int numRolls;
int[] dice1 = new int [1000];
int[] dice2 = new int [1000];
int[][] combo = new int[6][6];
public void getRolls()
{
System.out.println("Enter the number of times you want to roll a pair of dice: ");
numRolls = read.nextInt();
dice1 = new int[numRolls];
dice2 = new int[numRolls];
for (int i = 0; i < dice1.length; i++)
{
dice1[i] = diceRoll.nextInt(6) + 1;
dice2[i] = diceRoll.nextInt(6) + 1;
}
System.out.println("\n");
for (int j = 0; j < combo.length; j++)
{
for (int k = 0; k < combo[0].length; k++)
{
combo[j][k] = 0;
}
}
for (int m = 0; m < numRolls; m++)
{
combo[dice1[m] - 1][dice2[m] - 1]++;
System.out.println("You rolled: " + dice1[m] + " and " +
dice2[m] + " a total of " + combo[dice1[m] - 1][dice2[m] - 1] +
" times");
}
您应该将组合计算循环与打印循环分开。如果订单与您所说的不相关,那应该会为您提供所需的正确输出。编码愉快!
自答:
我将打印循环与组合计算循环分开了。
如果组合的组合值为 1,那么我只需将其打印出来,说明它已滚动 1 次。
如果组合的组合值大于 1,我会在第一次出现时将其打印出来,说明它已滚动多次,然后将该组合的组合值设置为 0。只有组合值至少为 1 的组合已打印,因此无法打印重复行(即 1,1 滚动 4 次现在仅打印在一行上而不是 4 行)。
for (int m = 0; m < numRolls; m++)
{
combo[dice1[m] - 1][dice2[m] - 1]++;
}
for (int m = 0; m < numRolls; m++)
{
if (combo[dice1[m] - 1][dice2[m] - 1] > 1)
{
System.out.println("You rolled: " + dice1[m] + " and " + dice2[m] + " a total of " + combo[dice1[m] - 1][dice2[m] - 1] + " time(s)");
combo[dice1[m] - 1][dice2[m] - 1] = 0;
}
if (combo[dice1[m] - 1][dice2[m] - 1] == 1)
{
System.out.println("You rolled: " + dice1[m] + " and " + dice2[m] + " a total of " + combo[dice1[m] - 1][dice2[m] - 1] + " time(s)");
}
}
作业的目标是使用并行的一维数组,但也允许使用二维数组。
我可以打印出不同的组合,例如一对骰子掷出的 1,1(也称为蛇眼)。
尝试打印出每个组合的滚动次数而不打印组合的滚动次数是很困难的。
例如:
输入您想掷一对骰子的次数: 5
你掷出:1和5共1次-我不想要的
您掷出了:4和3共1次
您滚动:1 和 5 共 2 次 - 对于重复,这就是我要打印的全部内容
您掷出:3和3共1次
您掷出:2和2共1次
我知道在增加组合数组(它包含每个组合被滚动的次数)之后立即打印出来的循环是不正确的,但我不知道如何修改它。
我认为 combo[0][0] 是 1,1 的滚动次数,combo[0][1] 是 1,2 的滚动次数,依此类推。
import java.util.Scanner;
public class Dice {
Scanner read = new Scanner(System.in);
Random diceRoll = new Random();
int numRolls;
int[] dice1 = new int [1000];
int[] dice2 = new int [1000];
int[][] combo = new int[6][6];
public void getRolls()
{
System.out.println("Enter the number of times you want to roll a pair of dice: ");
numRolls = read.nextInt();
dice1 = new int[numRolls];
dice2 = new int[numRolls];
for (int i = 0; i < dice1.length; i++)
{
dice1[i] = diceRoll.nextInt(6) + 1;
dice2[i] = diceRoll.nextInt(6) + 1;
}
System.out.println("\n");
for (int j = 0; j < combo.length; j++)
{
for (int k = 0; k < combo[0].length; k++)
{
combo[j][k] = 0;
}
}
for (int m = 0; m < numRolls; m++)
{
combo[dice1[m] - 1][dice2[m] - 1]++;
System.out.println("You rolled: " + dice1[m] + " and " +
dice2[m] + " a total of " + combo[dice1[m] - 1][dice2[m] - 1] +
" times");
}
您应该将组合计算循环与打印循环分开。如果订单与您所说的不相关,那应该会为您提供所需的正确输出。编码愉快!
自答:
我将打印循环与组合计算循环分开了。 如果组合的组合值为 1,那么我只需将其打印出来,说明它已滚动 1 次。 如果组合的组合值大于 1,我会在第一次出现时将其打印出来,说明它已滚动多次,然后将该组合的组合值设置为 0。只有组合值至少为 1 的组合已打印,因此无法打印重复行(即 1,1 滚动 4 次现在仅打印在一行上而不是 4 行)。
for (int m = 0; m < numRolls; m++)
{
combo[dice1[m] - 1][dice2[m] - 1]++;
}
for (int m = 0; m < numRolls; m++)
{
if (combo[dice1[m] - 1][dice2[m] - 1] > 1)
{
System.out.println("You rolled: " + dice1[m] + " and " + dice2[m] + " a total of " + combo[dice1[m] - 1][dice2[m] - 1] + " time(s)");
combo[dice1[m] - 1][dice2[m] - 1] = 0;
}
if (combo[dice1[m] - 1][dice2[m] - 1] == 1)
{
System.out.println("You rolled: " + dice1[m] + " and " + dice2[m] + " a total of " + combo[dice1[m] - 1][dice2[m] - 1] + " time(s)");
}
}