Программа бросает определенное количество кубиков с определенной стороной определенное количество раз, и все это задается пользователем. Я занимаюсь отладкой и устранением неполадок, почему он не работает в течение 4 дней, и не могу понять это.
Я знаю, что проблема заключается в генерации случайного числа определенное количество раз (на каждый кубик, брошенный вокруг строки 38), но когда код компилируется так, как он находится ниже, все увеличенные значения массива кратны количеству игральных костей. которые должны использоваться (вводятся пользователем), и я не могу понять, как сделать программный цикл генератором случайных чисел для всего количества брошенных игральных костей, при этом все еще увеличивая значения массива.
package DiceRolls_2;
import java.util.Scanner;
import java.lang.Math;
public class DiceRolls_2
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int numRolls; //number of rolls
int outcome; //index value to be +1'd
int numDice; //number of dice to be thrown
int numSides; //number of sides per die
int arraySize; //size of array (dependant on number of sides)
int minRoll; //minimum roll value (number of die (each one rolls 1)
int maxRoll; //maximum roll value (number of sides (each side increments +1 to total roll possibility))
/* prompt user for number of rolls */
System.out.print("How many rolls? ");
numRolls = input.nextInt(); //sets number of rolls
System.out.print("How many dice to roll? ");
numDice = input.nextInt();
System.out.print("How many sides per die? ");
numSides = input.nextInt();
maxRoll = numSides*numDice; //max roll is equal to num sides because the highest value number is the latest (highest) side number
minRoll = numDice; //minimum roll is equal to number of dice since each dice has a 1 value
arraySize = maxRoll + 1; //size of array depends on the maximum roll value + 1 for positioning of index values
int[] outcomes = new int[arraySize];
/* roll dice and add to outcomes */
for (int roll = 0; roll <= numRolls; roll++)
{
int numberRolled = (int) (numSides * Math.random() + 1);
outcome = numberRolled;
outcomes[outcome] += 1;
}
/* show counts of outcomes */
for (int i = minRoll; i <= maxRoll; i++) //3 is the minimum number (1) all number of dice can roll (dependant on number of dice are being rolled)
{ //18 is maximum number all 3 dice (6 per die) can roll (dependant on number of dice are being rolled)
System.out.println(i + ": " + outcomes[i]); //prints all array values with number of occurances
}
input.close();
}
}




Количество результатов - это количество кубиков, умноженное на количество ролей (а не на количество сторон). Я немного упростил ваш код, вам нужен только один цикл для хранения и печати (например), и ваши объявления переменных можно переместить туда, где они нужны. Нравиться,
Scanner input = new Scanner(System.in);
System.out.print("How many rolls? ");
int numRolls = input.nextInt();
System.out.print("How many dice to roll? ");
int numDice = input.nextInt();
System.out.print("How many sides per die? ");
int numSides = input.nextInt();
int[] outcomes = new int[numDice * numRolls];
for (int roll = 0; roll < outcomes.length; roll++) {
outcomes[roll] = 1 + (int) (numSides * Math.random());
System.out.printf("%d: %d%n", roll, outcomes[roll]);
}
Думаю, нужен вложенный цикл. Внутри цикла для
numRollsдобавьте цикл дляnumDice.