简体   繁体   English

使用一维数组编写一个程序,该数组具有来自随机生成器的值,以打印出每个组合被一对骰子滚动多少次

[英]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.

相关问题 程序滚动骰子次数(使用数组),并且必须显示数字滚动的次数(除非它不起作用)) - Program rolls dice number of times(Using arrays), and must display the number of times a number rolled (Except it doesn't work)) 角色2骰子100000次,写出每个总和滚动的次数并制作图表-Java - Role 2 dice 100000 times, write the amount of times each sums and rolled and make a graph - Java 如何将Pascal Triangle Generator从2-D数组转换为1-D数组? - How to convert Pascal Triangle Generator from 2-D to 1-D arrays? 如何使方法打印出特定数字的滚动次数? - how to make a method print out the amount of times a specific number was rolled? 如何打印出两个六个双面骰子加到4的次数 - How to print out the number of times two six sided dice add to 4 从二维数组打印随机值,这些值来自其他一维 arrays(应该是字符串) - Print Random Values from an 2D Array, these values coming from other 1D arrays (should be strings) 在Java中找到两个随机数之间的平均值,然后计算使用switch语句将一个数字滚动多少次 - find average between two random numbers in java and then count how many times a number was rolled with a switch statement 使用随机数生成器记录一个数字出现的次数。 Java - Keeping a tally of how many times a number appears using a random number generator. Java 随机骰子生成器 - Random Dice Generator 获取用户输入,放入数组并打印出每个字母的使用次数 - Take user input, put into an array and print out how many times each letter is used
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM