Write a program that tests your ESP (extrasensory perception). The program shoul
ID: 3543298 • Letter: W
Question
Write a program that tests your ESP (extrasensory perception). The program should randomly select the name of a color from the following list of words:
Red, Green, Orange, Yellow
To select a word, the program can generate a random number. For example, if the number is 0, the selected word is Red; if the number is 1, the selected word is Green; and so forth.
Next, the program should ask the user to enter the color that the computer has selected. After the user has entered his or her guess, the program should display the name of the randomly selected color. The program should repeat this 10 times and then display the number of times the user correctly guessed the selected color. Be sure to modularize the program into methods that perform each major task.
Explanation / Answer
please rate - thanks
any questions ask
import java.util.*;
public class main
{public static void main(String args[])
{
int right=0;
right=doLoop();
report(right);
}
public static int doLoop()
{int count=0,n;
String user;
Random r=new Random();
Scanner in=new Scanner(System.in);
for(int i=0;i<10;i++)
{n=r.nextInt(4);
menu();
user=in.next();
if(myChoice(n).compareToIgnoreCase(user)==0)
count++;
}
return count;
}
public static void menu()
{System.out.print("What is my color: ");
System.out.print("red ");
System.out.print("green ");
System.out.print("orange ");
System.out.print("yellow ");
}
public static void report(int right)
{System.out.println("You were correct "+right+" times");
}
public static String myChoice(int n)
{String color;
switch(n)
{case 0: color="RED"; break;
case 1: color="GREEN"; break;
case 2: color="ORANGE"; break;
default: color="YELLOW"; break;
}
System.out.println("I chose "+color);
return color;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.