### Thread: Dice Rolling

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
10
Rep Power
0

#### Dice Rolling

Hi everyone,

I am taking Java for the first time(required for my degree), and I am looking for help with a program I need to create. I have searched the forums, but none of them meet the criteria that I am looking for.

For my homework, I need to write a program that will roll 2 die based on the user input. The program needs to keep looping until the number of rolls has been reached. For my output, I need to display the roll number, the roll of the 2 die, and the sum of the 2 die.
For example, if the user entered to have the program roll the die 10 times, I want my output to look something like this:

Roll 1 - The first die comes up 5, the second die comes up 4, your total is 9.
Roll 2 - The first die comes up 2, the second die comes up 3, your total is 5.
Roll 3 - The first die comes up 6, the second die comes up 1, your total is 7.
This will continue to go until the max roll is reached. In this case 10.

Can anyone help me out?
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
10
Rep Power
0
import javax.swing.JOptionPane;

public class Dice
{

public static void main(String[] args)
{
int die1;
int die2;
int roll;

die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
roll = die1 + die2;

JOptionPane.showInputDialog( null,
"How many times do you want to roll the die? From 1-100.");
}
}

This is what I have so far.
3. Looks like you're almost there.

You should place the question first then have a (for-)loop to repeat the dice rolls.

You also have to show the result, which can be done the the console (system.out.println) or in a dialog (showMessageDialog).
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
10
Rep Power
0
Would I need to create another variable for the user input, and how would I tie my (for) loop to the user input?
5. The function showInputDialog return a String value, which does need its own variable.
To use the input in your loop, you will have to parse it to an integer.

Regarding the loop, you should read about the for-loop, if you haven't already done this.

basically, you program logic will be something like this:
For 1 to numberOfRolls
// Do all the dice thing: roll, add and view
End For
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
10
Rep Power
0
import java.lang.System;
import javax.swing.JOptionPane;

public class Dice
{

public static void main(String[] args)
{
int die1;
int die2;
int roll;
int input = 0;

die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
roll = die1 + die2;

input = Integer.parseInt(JOptionPane.showInputDialog(
null, "How many times do you want to roll the die? From 1-100."));
for(int i=0;i<input;i++)
{

System.out.print("The first die comes up " + die1);
System.out.print(", the second die comes up " + die2);
System.out.print(", the total is " + roll);
System.out.print(".");
System.out.print("\n");
}
}

}
Now when I run my program, it just duplicates the roll of the first output. It doesn't generate new rolls.
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
10
Rep Power
0
import java.lang.System;
import javax.swing.JOptionPane;

public class Dice
{

public static void main(String[] args)
{
int die1;
int die2;
int roll;
int input = 0;

input = Integer.parseInt(JOptionPane.showInputDialog(
null, "How many times do you want to roll the die? From 1-100."));
for(int i=0;i<input;i++)
{
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
roll = die1 + die2;

System.out.print("The first die comes up " + die1);
System.out.print(", the second die comes up " + die2);
System.out.print(", the total is " + roll);
System.out.print(".");
System.out.print("\n");
}
}

}

I think I got it!!
8. looks correct, maybe you have to add the roll number (variable i).

To take the learning one step forward, try enter a letter instead of number as input.
When you see the result, you can then look up try-catch.