Hi, im having trouble writing java code for this assignment. Instructions: Write
ID: 3542854 • Letter: H
Question
Hi, im having trouble writing java code for this assignment.
Instructions:
Write code that plays a simple dice game betweeen you and the computer. The dice you wil be using are very sepcial dice that have 20 sides instead of the usual 6 sided die. When the program runs, a loop should repeat 17 times. (you must use a for loop).
* Generate a random number in the range of 1 through 20. (Computers die)
* Generate a random number in the range of 1 through 20. (Users die)
* The die with the highest value wins. Display this result to the screen.
* Program should also keep count of the number of times the computer wins, and the number of times the user wins.
* Pring out if thagame was a tie.
* Afterloop iterates, the program should display who was the grand winniner.
I would be very thankful if I could get help on how this code looks and how to start it off.
Explanation / Answer
import java.util.*;
import java.util.Random;
public class test4 {
public static void main(String args[])
{
Random randomGenerator = new Random();
int comp_win=0,iwin=0,tie=0,i;
for (i=0;i<17;i++)
{
int randomInt1 = randomGenerator.nextInt(20);
System.out.println("the computer die value:"+randomInt1);
int randomInt2 = randomGenerator.nextInt(20);
System.out.println("the user die value:"+randomInt2);
if(randomInt1>randomInt2)
{
comp_win++;
System.out.println("computer wins");
}
else if(randomInt1<randomInt2)
{
iwin++;
System.out.println("user wins");
}
else if(randomInt1==randomInt2)
{
tie++;
System.out.println("there is tie");
}
System.out.println();
System.out.println();
}
if(comp_win>iwin)
System.out.println("the grand winner is : computer");
else if(comp_win<iwin)
System.out.println("the grand winner is :user");
else
System.out.println("overall there is a tie");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.