Hi, im having trouble writing java code for this assignment. * Afterloop iterate
ID: 3542867 • Letter: H
Question
Hi, im having trouble writing java code for this assignment.
* Afterloop iterates, the program should display the total number of wins the computer had, the total number of wins the user had, and the total number of ties In the event of a tie the program will state "Tie Game!"
Im not sure how to get the above question into my code.
This code below is what i have and it is pretty much done, just need to add that part.
Thank you in advance.
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");
}
}
Explanation / Answer
Modified code
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();
}
System.out.println("Total number of computer win :"+comp_win);
System.out.println("Total number of user win :"+iwin);
System.out.println("Total number of ties :"+tie);
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.