Write a program that plays a simple dice game between the computer and the user.
ID: 3714165 • Letter: W
Question
Write a program that plays a simple dice game between the computer and the user. When the program runs, a loop will repeat 10 times. Each iteration of the loop should do the following:
Generate a random integer in the range of 1 through 6. This is the value of the computer's die.
Generate a random integer in the range of 1 through 6. This is the value of the user's die.
The die with the highest value wins. (In case of a tie, there is no winner for that particular roll of the dice.)
As the loop iterates, the program should keep count of the number of times the computer wins, and the number of times that the user wins. After the loop performs all of it's iterations, the program should display who has the higher number of wins, the computer or the user.
Explanation / Answer
Below is your code in Java. Let me know in comments if you have any issues //DiceComputerUser.java import java.util.Random; public class DiceComputerUser { public static void main (String [] args) { Random randGen = new Random(); int computer = 0; int user = 0; int roll1, roll2; for (int i = 0; i < 10; i++){ roll1 = randGen.nextInt(6); roll2 = randGen.nextInt(6); if(roll1 > roll2){ computer++; } if(roll1 user){ System.out.println("Computer won!"); } else if(user > computer){ System.out.println("User won!"); } else{ System.out.println("Tie!"); } } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.