In Java please 1. Dice Game (20 points) Write a program that plays a simple dice
ID: 3677803 • Letter: I
Question
In Java please
1. Dice Game (20 points) Write a program that plays a simple dice game between the computer and the user. When the program runs, a loop should 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 computer's die Generate another random integer in the range of 1 through 6. This is the value of 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 its iterations, the program should display who was the grand winner, the computer or the user OUTPUT FORMAT: Round Computer's Die User's Die Winner User Computer Tie 10 User continued next page Total Rounds won by Computer Total Rounds won by User Grand Winner isExplanation / Answer
java program:
import java.io.*;
import java.util.Random;
public class DieGame {
public static void main(String[] args)throws IOException
{
int userwin=0,comwin=0,size=10,comdie=0,userdie=0;
Random ran=new Random();
System.out.println("Round Computer's Die User's Die Winner");
for(int i=0;i<size;i++)
{
comdie=ran.nextInt(6)+1;
userdie=ran.nextInt(6)+1;
if(comdie>userdie)
{comwin++;
System.out.println((i+1)+" "+comdie+" "+userdie+" Computer");
}
else if(comdie<userdie)
{userwin++;
System.out.println((i+1)+" "+comdie+" "+userdie+" User");
}
else
{
System.out.println((i+1)+" "+comdie+" "+userdie+" Tie");
}
}
System.out.println("Total Rounds won by Computer:"+comwin);
System.out.println("Total Rounds won by User:"+userwin);
if(comwin>userwin)
System.out.println("Grand Winner is: Computer");
else if(comwin<userwin)
System.out.println("Grand Winner is: User");
else
System.out.println("Grand Winner is: Tie");
}
}
output:
run:
Round Computer's Die User's Die Winner
1 4 4 Tie
2 1 4 User
3 5 6 User
4 6 1 Computer
5 6 2 Computer
6 4 4 Tie
7 5 1 Computer
8 1 4 User
9 4 6 User
10 2 1 Computer
Total Rounds won by Computer:4
Total Rounds won by User:4
Grand Winner is: Tie
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.