Hi everyone, I've been taking a Intro to Java class this year and been enjoying it, I recently had a assignment which was a simple dice game, however I was wondering if a few tweeks could be made.

  1. Could we ask the user how many dice they want to roll? If so how could that be implemented.
  2. Check if the outcomes are as expected.

I've been enjoying Java alot and would appreciate some help with these two questions and I'm sure it can be done! I just don't have the expertise to know what to do, but will one day hopefully but a little help :) thanks.

Heres what I got so far, but it's only for one die.

public class DiceGame {
    private static Scanner input;

    public static void main(String[] args) {

        // Declare variables for Scanner and Random
        input = new Scanner(System.in);
        Random random = new Random();

        // Setup an array to hold the values from rolls. Declare a variable to hold the user's input,
        // and another variable to hold the number generated by java.util.Random.
        int[] frequency = new int[6];
        int rolls;
        int temp;

        // Asks user for input on how many rolls they would like to make.
        System.out.print("How many times do you want to roll the dice\n");
        rolls = input.nextInt();

        // for loop to go through the number of rolls user inputs.
        for(int i = 0; i < rolls; i++) {
            temp = random.nextInt(6);
            frequency[temp]++;
        }

        // Display the number of rolls and results with a for loop.
        System.out.print("Face\tFrequency");
        for(int i = 0; i < 6 ; i++) {
            System.out.println(i+1 + "\t" + frequency[i]);
        }
    }
}

Ask the user for the amount of dice they want to use ( let's say n number of dice). So now you will need to change your for loop. You will require nested for loops (a for loop within a for loop).

The outer loop will be the iterations until n number of dice.
The inner loop will be same as you have in your code.

Hope that helps :)

It did thanks but I'm a little stuck on the iterations part, I was able to get it to do two dice, as you can se just a simple if else, but I can kinda see how I would need to expand greatly here, (the nested for loops your talking about) gona do some more research and see if I can figure that out, thanks so much. heres my progress right now.

public class DiceGame2 {
    private static Scanner input;

    public static void main(String[] args) {

        // Declare variables for Scanner and Random
        input = new Scanner(System.in);
        Random random = new Random();

        // Setup an array to hold the values from rolling one or two dice.
        // Declare a variable to hold the user's input for rolls and dice.
        // and another variable to hold the number generated by java.util.Random.
        int[] frequency = new int[6];
        int[] frequency2 = new int[12];
        int rolls;
        int dice;
        int temp;

        // Asks user how many die they would like to roll, then stores it in the variable dice.
        System.out.print("How many die would you like to roll, 1 or 2?\n");
        dice = input.nextInt();

        if(dice == 1) {

           // Asks user for input on how many rolls they would like to make, then stores it in the variable rolls.
           System.out.print("How many times do you want to roll the dice\n");
           rolls = input.nextInt();

           // for loop to go through the number of rolls user inputs.
           for(int i = 0; i < rolls; i++) {
               temp = random.nextInt(6);
               frequency[temp]++;
           }

           // Display the number of rolls and results with a for loop.
           System.out.print("Face\tFrequency\n");
           for(int i = 0; i < 6 ; i++) {
              System.out.println(i+1 + "\t" + frequency[i]);
           }

        }else{
               // Asks user for input on how many rolls they would like to make, then stores it in the variable rolls.
               System.out.print("How many times do you want to roll the dice\n");
               rolls = input.nextInt();

               // for loop to go through the number of rolls user inputs for two dice
               for(int i = 0; i < rolls; i++) {
                   temp = random.nextInt(12);
                   frequency2[temp]++;
               }

               // Display the number of rolls and results with a for loop of two dice results combined.
               System.out.print("Face\tFrequency\n");
               for(int i = 0; i < 11 ; i++) {
                  System.out.println(i+2 + "\t" + frequency2[i]);
               }
        }
    }
}

Still unsure how to check if the results are as expected.

What do you mean by "results" are as expected? Do you mean checking with the probability of getting each side?

since the results are supposed to be random (roll of the dice) there is no such thing as 'expected results'.

don't use if-else for iterations, use an actual loop for that:

do-while, while,
for

....

it's not as if you haven't got any options. using if else blocks for this, 'll make your code needlessly difficult to read/maintain.

Trying to figure out how to do a nested if statement for the number of dice having some issues finding a solution

didnt you do that on your second post to this thread? Please provide new code if you ve changed your previouse code a be more specific, another thing that i can see in your code is that you re are reaching number 12 with the second dice, shouldn it be 6 again?

import java.util.Random;
import java.util.Scanner;

public class DiceGame2 {
    private static Scanner input;
    public static void main(String[] args) {
        // Declare variables for Scanner and Random
        input = new Scanner(System.in);
        Random random = new Random();
        // Setup an array to hold the values from rolling one or two dice.
        // Declare a variable to hold the user's input for rolls and dice.
        // and another variable to hold the number generated by java.util.Random.
        int[] frequency = new int[6];
        int[] frequency2 = new int[6];
        int rolls;
        int dice;
        int temp;
        // Asks user how many die they would like to roll, then stores it in the variable dice.
        System.out.print("How many dice would you like to roll, 1 or 2?\n");
        dice = input.nextInt();
        if(dice == 1) {
           // Asks user for input on how many rolls they would like to make, then stores it in the variable rolls.
           System.out.print("How many times do you want to roll the dice\n");
           rolls = input.nextInt();
           // for loop to go through the number of rolls user inputs.
           for(int i = 0; i < rolls; i++) {
               temp = random.nextInt(6);
               frequency[temp]++;
           }
           // Display the number of rolls and results with a for loop.
           System.out.print("Face\tFrequency\n");
           for(int i = 0; i < 6 ; i++) {
              System.out.println(i+1 + "\t" + frequency[i]);
           }
        }else
        {
               // Asks user for input on how many rolls they would like to make, then stores it in the variable rolls.
               System.out.print("How many times do you want to roll the dice\n");
               rolls = input.nextInt();
               // for loop to go through the number of rolls user inputs for two dice
               for(int i = 0; i < rolls; i++) 
               {
                   temp = random.nextInt(6);

                   frequency[temp]++;
               }
               for(int i = 0; i < rolls; i++) 
               {
                   temp = random.nextInt(6);
                   frequency2[temp]++;

               }

               // Display the number of rolls and results with a for loop of two dice results combined.
               int counter=0;
               System.out.print("Face\tFrequency\n");
               System.out.println("Dice number1");
               for(int i = 0; i <frequency.length  ; i++) 
               {

                  System.out.println(i+1 + "\t" + frequency[i]);

               }
               System.out.println("Dice number2");
               for(int i = 0; i <frequency2.length ; i++) 
               {
                   System.out.println(i+1+"\t"+frequency2[i]);
               }
        }
        }
    }

I think that this is what you want, if this is what you want then you should make a method to short out this code and improve efficiency.

looks good but I need both rolls to be combined together for the a total of 12. So two things I still need to do, firstly how could I get the results to print out a histogram isntead of just numbers like maybe stars? and I still a little lost on how to do the nested for loops for allowing more than two dice. I'll do all the work! just need a nudge in the right direction :) plz and thanks

This is what I got so far.

import java.util.Scanner;
import java.util.Random;

public class DiceGame2 {
    private static Scanner input;

    public static void main(String[] args) {

        // Declare variables for Scanner and Random
        input = new Scanner(System.in);
        Random random = new Random();

        // Setup an array to hold the values from rolling one or two dice.
        // Declare a variable to hold the user's input for rolls and dice.
        // and another variable to hold the number generated by java.util.Random.
        int[] frequency = new int[6];
        int[] frequency2 = new int[12];
        int rolls;
        int dice;
        int temp;

        // Asks user how many die they would like to roll, then stores it in the variable dice.
        System.out.print("How many die would you like to roll, 1 or 2?\n");
        dice = input.nextInt();

        if (dice >= 3) {
              System.out.println("You have entered a invalid selection please try again.\n");
              System.out.print("How many die would you like to roll, 1 or 2?\n");
              dice = input.nextInt();
        }

        if(dice == 1) {

           // Asks user for input on how many rolls they would like to make, then stores it in the variable rolls.
           System.out.print("How many times do you want to roll the dice\n");
           rolls = input.nextInt();

           // for loop to go through the number of rolls user inputs.
           for(int i = 0; i < rolls; i++) {
               temp = random.nextInt(6);
               frequency[temp]++;
           }

           // Display the number of rolls and results with a for loop.
           System.out.print("Face\tFrequency\n");
           for(int i = 0; i < 6 ; i++) {
              System.out.println(i+1 + "\t" + frequency[i]);
           }

         } else {
               // Asks user for input on how many rolls they would like to make, then stores it in the variable rolls.
               System.out.print("How many times do you want to roll the dice\n");
               rolls = input.nextInt();

               // for loop to go through the number of rolls user inputs for two dice
               for(int i = 0; i < rolls; i++) {
                   temp = random.nextInt(12);
                   frequency2[temp]++;
               }

               // Display the number of rolls and results with a for loop of two dice results combined.
               System.out.print("Face\tFrequency\n");
               for(int i = 0; i <= 10 ; i++) {
                  System.out.println(i+2 + "\t" + frequency2[i]);
               }
         }
      }
   }

solved thanks for the advice everyone

commented: well done! +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.