I've got an assignment that requires me to have the user guess a random color generated by the program, then display the total results. My code works fine, but I'm wanting to break it down into another method. Specifically, the section that receives input from the user. I want that in its own method, then the main method only displaying results. I tried creating a new object for the user's color choice, but that didn't work. I'm not sure what to do next. Thank you for your time!
import java.util.Scanner;
import java.util.Random;
public class ESP
{
public static void main(String[] args)
{
String colorChoice;
int correct = 0, incorrect = 0;
Scanner keyboard = new Scanner(System.in);
for(int i = 1; i <= 10; i++)
{
System.out.print("Choose either red, green, blue, orange or yellow! ");
colorChoice = keyboard.nextLine();
System.out.println("You chose: " + colorChoice);
String computerChoice = computerColorChoice();
System.out.println("The computer chose: " + computerChoice);
if(colorChoice.equalsIgnoreCase(computerChoice))
{
correct++;
}
else
{
incorrect++;
}
}
System.out.println("Amount of times you guessed correctly: " + correct);
System.out.println("Amount of times you guess incorrectly: " + incorrect);
}
public static String computerColorChoice()
{
String randomColor;
int randomNumber;
Random random = new Random();
randomNumber = random.nextInt(4);
switch (randomNumber)
{
case 0:
randomColor = "Red";
break;
case 1:
randomColor = "Green";
break;
case 2:
randomColor = "Blue";
break;
case 3:
randomColor = "Orange";
break;
case 4:
randomColor = "Yellow";
break;
default:
randomColor = " ";
}
return randomColor;
}
}