1

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

  • Java, not JavaScript. Pretty please understand that the two languages have [nothing to do with one another](http://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java). – apsillers Oct 31 '12 at 02:12
  • Why do you need to keep a list of rolls around? Why not just make new rolls as you need them? – DigitalGhost Oct 31 '12 at 03:17
  • It was the only way I found myself able to put my idea into the program. I made the list because I need to output the sum of the rolls at the end, and the individual rolls. I couldn't conceptually think of how to do it without a list and making new rolls as I need them. – user1787251 Oct 31 '12 at 03:21

1 Answers1

0

You may probably want to use another loop outside your while loop, if you want to repeat the #dice for all the players.

The for(int i:rolls) --> this statement is read as "for each integer 'i' in rolls" which means, for every iteration of the loop, the values in rolls are assigned to y:

And this is equivalent to

for(int j=0;j<rolls.size();j++){
   i = rolls[j];
   // Other statements goes here.
}
sravanreddy001
  • 257
  • 1
  • 5
  • 17
  • first of all thanks for the quick response. I didn't even realize the comments I left in the code, funny little stuff I type when im confused/stuck and frustrated. I really appreciate you explaining the int i:rolls line, it really helped. The equivalent statement seems even simpler. What I'm stuck on is what to put in this other loop that will make the program roll the number of dice for each player. I wanted to have the program make a new arraylist for each player and assign the rolls to each list, but I have NO clue how... any thoughts? – user1787251 Oct 31 '12 at 02:28
  • Also the code you posted for an alternative gives an error "array required, arraylist found". – user1787251 Oct 31 '12 at 02:55