I am trying to make a program that takes an input for #players
and #dice
and rolls the dice whatever number of times for each player. It then outputs the rolls and the total.
So far I've managed to develop a program that can roll as many dice as inputted and store these values in an array, which it then sums and outputs.
Unfortunately, I'm stuck as of now because I don't really have any clue what to do when trying to make the program do this again each time for a new player. I understand it would probably be with an incrementer, but I'm just really overwhelmed by the complexity and don't even know what I would look for online.
Here is my code:
package diceroll;
import java.util.ArrayList;
import java.util.Scanner;
public class DiceRoll {
public static void main(String[] args) {
int numplayers = 0, numdice = 0; // incrementers for #rolls and #players
// ArrayList<ArrayList> players = new ArrayList<ArrayList>();
// players.add(rolls); /// adding list to a list
// System.out.println(players);
ArrayList<Integer> rolls = new ArrayList<>();
System.out.println("Enter the number of players.");
Scanner scan = new Scanner (System.in);
numplayers = scan.nextInt();
System.out.println("Enter the number of dice.");
numdice = scan.nextInt();
while (numdice > 0 ) {
Die die1 = new Die();
die1.roll();
rolls.add(die1.getFaceValue());
numdice--;}
System.out.println(rolls);
// sum for array, but i cant access the arraylength
int total = 0;
for (int n : rolls) //what does the colon : do ?
{total += n;
System.out.println("Dice total:" + total);
}
}
}
There is also a basic Die.java
class that assigns a random number to face value and has the roll method I use to randomize the die.
Output:
run: Enter the number of players. 1 Enter the number of dice. 4 [5, 4, 6, 6] 5 9 15
The only problem is changing the number of players currently has no effect. 21