// dice_loop1: rolling a die and using "if" to ascertain the result in your game! // run 1000 times and check the results // see http://cs.nyu.edu/courses/fall03/V22.0002-003/class8_random_dice1.txt public class dice_loop { public static void main(String[] args) { int roll; int ones = 0; int twos = 0; int threes = 0; int fours = 0; int fives = 0; int sixes = 0; int others = 0; int counter = 1; int stop = 1000; while (counter <= stop) { roll = 1 + (int) (Math.random() * 6); System.out.println("Roll #: " + counter); if (roll == 1 ) ones++; else if (roll == 2 ) twos++; else if (roll == 3 ) threes++; else if (roll == 4 ) fours++; else if (roll == 5 ) fives++; else if (roll == 6 ) sixes++; else others++; counter++; } // end of while loop // print out the results System.out.println("The computer rolled a 1 " + ones+ " times."); System.out.println("The computer rolled a 2 " + twos+ " times."); System.out.println("The computer rolled a 3 " + threes+ " times."); System.out.println("The computer rolled a 4 " + fours+ " times."); System.out.println("The computer rolled a 5 " + fives+ " times."); System.out.println("The computer rolled a 6 " + sixes+ " times."); System.out.println("The computer rolled a die a total of " + (ones+twos+threes+fours+fives+sixes+others) + " times."); System.exit (0); } }