How do I change this to a do while loop?? Please do not add extra stuff, just te
ID: 3563833 • Letter: H
Question
How do I change this to a do while loop?? Please do not add extra stuff, just tell me how to change to do while..Thank You
public static void main(String[] args)
{
Random generator = new Random();
final int NUMBER = 10000; //the number of times to
//roll the dice
int count = 0; // number of times the dice were
// rolled
int snakeEyes = 0; // number of times snake eyes
// rolled
int twos = 0; // number of times double two is
// rolled
int threes = 0; // number of times double three
// is rolled
int fours = 0; // number of times double four
// is rolled
int fives = 0; // number of times double
// five is rolled
int sixes = 0; // number of times double six is
// rolled
while (count < NUMBER)
{
int die1Value = generator.nextInt(6); //random number 0..5
die1Value += 1;
System.out.println("Die one value is " + die1Value);// number of spots on the first
int die2Value = generator.nextInt(6);
die2Value += 1;
System.out.println("Die two value is " + die2Value);// number of spots on the second die
if (die1Value == die2Value)
if (die1Value == 1)
++snakeEyes;
else if (die1Value == 2)
++twos;
else if (die1Value == 3)
++threes;
else if (die1Value == 4)
++fours;
else if (die1Value == 5)
++fives;
else if (die1Value == 6)
++sixes;
++count;
}
System.out.println ("You rolled snake eyes " +
snakeEyes + " out of " + count + " rolls.");
System.out.println ("You rolled double twos " + twos +
" out of " + count + " rolls.");
System.out.println ("You rolled double threes " +
threes + " out of " + count + " rolls.");
System.out.println ("You rolled double fours " + fours
+ " out of " + count + " rolls.");
System.out.println ("You rolled double fives " + fives
+ " out of " + count + " rolls.");
System.out.println ("You rolled double sixes " + sixes
+ " out of " + count + " rolls.");
}
}
Explanation / Answer
import java.util.*;
public class Dier{
public static void main(String[] args)
{
Random generator = new Random();
final int NUMBER = 10000; //the number of times to
//roll the dice
int count = 0; // number of times the dice were
// rolled
int snakeEyes = 0; // number of times snake eyes
// rolled
int twos = 0; // number of times double two is
// rolled
int threes = 0; // number of times double three
// is rolled
int fours = 0; // number of times double four
// is rolled
int fives = 0; // number of times double
// five is rolled
int sixes = 0; // number of times double six is
// rolled
do
{
int die1Value = generator.nextInt(6); //random number 0..5
die1Value += 1;
System.out.println("Die one value is " + die1Value);// number of spots on the first
int die2Value = generator.nextInt(6);
die2Value += 1;
System.out.println("Die two value is " + die2Value);// number of spots on the second die
if (die1Value == die2Value)
if (die1Value == 1)
++snakeEyes;
else if (die1Value == 2)
++twos;
else if (die1Value == 3)
++threes;
else if (die1Value == 4)
++fours;
else if (die1Value == 5)
++fives;
else if (die1Value == 6)
++sixes;
++count;
}while (count < NUMBER);
System.out.println ("You rolled snake eyes " +
snakeEyes + " out of " + count + " rolls.");
System.out.println ("You rolled double twos " + twos +
" out of " + count + " rolls.");
System.out.println ("You rolled double threes " +
threes + " out of " + count + " rolls.");
System.out.println ("You rolled double fours " + fours
+ " out of " + count + " rolls.");
System.out.println ("You rolled double fives " + fives
+ " out of " + count + " rolls.");
System.out.println ("You rolled double sixes " + sixes
+ " out of " + count + " rolls.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.