[英]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
The goal of the assignment is to use parallel 1-D arrays, but 2-D arrays are also allowed. 分配的目标是使用并行一维数组,但也允许二维数组。
I can print out the different combinations such as 1,1 (otherwise known as snake eyes) rolled by the pair of dice. 我可以打印出不同的组合,例如一对骰子滚动的1,1(也称为蛇眼)。
Trying to print out the number of times each combination was rolled without printing the combination the same number of times it was rolled is difficult. 试图打印每个组合的滚动次数而不打印组合的相同次数很难。
Ex: 例如:
Enter the number of times you want to roll a pair of dice: 5 输入要掷骰子的次数:5
You rolled: 1 and 5 a total of 1 times - What I don't want 您滚动了:1和5,总共1次-我不想要的
You rolled: 4 and 3 a total of 1 times 您滚动了:4和3,共1次
You rolled: 1 and 5 a total of 2 times - For duplicates this is all I want to print 您滚动了:1和5,共2次 -对于重复,这就是我要打印的全部
You rolled: 3 and 3 a total of 1 times 您滚动:3和3,共1次
You rolled: 2 and 2 a total of 1 times 您滚动了:2和2,共1次
I know the loop for printing it out right after it increments the combo array (which holds the number of times each combination was rolled) is not correct, but I am stuck on how to modify it. 我知道在递增组合数组(保存每个组合滚动的次数)后立即打印出来的循环是不正确的,但是我仍然坚持如何修改它。
I consider combo[0][0] to be the number of times 1,1 is rolled, combo[0][1] to be the number of times 1,2 is rolled, and so on. 我认为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");
}
You should separate the combo calculation loop from the printing loop. 您应该将组合计算循环与打印循环分开。 If the order is not relevant as you stated, that should give you the correct output you're looking for.
如果订单与您所说的不相关,则应该为您提供所需的正确输出。 Happy coding!
编码愉快!
Self-answer: 自答案:
I made the printing loop separate from the combo calculation loop. 我将打印循环与组合计算循环分开。 If the combo value for the combination is 1, then I simply print it out stating it was rolled 1 time.
如果组合的组合值是1,那么我只需将其打印出来,说明已滚动1次。 If the combo value for the combination was greater than 1, I print it out at the first occurrence stating it was rolled that many times, then set the combo value for that combination equal to 0. Only combos with at least a combo value of 1 are printed, so duplicate lines cannot be printed (ie 1,1 rolled 4 times only prints on one line now instead of 4 separate lines).
如果该组合的组合值大于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)");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.