So I'm trying to add a loop to this program that will display an error message to the user if they enter a color that isn't red, green, blue, orange, or yellow. I'm just not sure on how to do so. I've tried adding a while loop in the userColorChoice
method, but it just creates an infinite loop - so I'm doing something wrong. Thank you!
import java.util.Scanner;
import java.util.Random;
public class ESP
{
public static void main(String[] args)
{
int correct = 0, incorrect = 0;
for(int i = 1; i <= 10; i )
{
if(userColorChoice().equalsIgnoreCase(computerColorChoice()))
{
System.out.println("You guessed correctly!");
correct ;
}
else
{
System.out.println("Sorry, your guess was wrong!");
incorrect ;
}
}
System.out.println("Amount of times you guessed correctly: " correct);
System.out.println("Amount of times you guess incorrectly: " incorrect);
}
public static String userColorChoice()
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Choose either red, green, blue, orange or yellow! ");
String userColorChoice = keyboard.nextLine();
System.out.println("You chose: " userColorChoice);
return userColorChoice;
}
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 = " ";
}
System.out.println("The computer chose: " randomColor);
return randomColor;
}
}
CodePudding user response:
I would suggest you introduce an enum
COLOR
Also change the computerColorChoice
method to something like below, this way you do not have to add the code every time you add a new color
I added this new line to COLOR.valueOf(capitaliseFirstLetter(userColorChoice));
if userColorChoice
not one of the colors this would throw IllegalArgumentException
that is being handled in main method catch block
public static void main(String[] args) {
int correct = 0, incorrect = 0, i = 0;
while (i < 10) {
try {
if (userColorChoice().equalsIgnoreCase(computerColorChoice())) {
System.out.println("You guessed correctly!");
correct ;
} else {
System.out.println("Sorry, your guess was wrong!");
incorrect ;
}
i ;
} catch (IllegalArgumentException e) {
System.out.println("invalid choice");
}
}
System.out.println("Amount of times you guessed correctly: " correct);
System.out.println("Amount of times you guess incorrectly: " incorrect);
}
public static String userColorChoice() {
Scanner keyboard = new Scanner(System.in);
System.out.print("Choose either red, green, blue, orange or yellow! ");
String userColorChoice = keyboard.nextLine();
COLOR.valueOf(capitaliseFirstLetter(userColorChoice));
System.out.println("You chose: " userColorChoice);
return userColorChoice;
}
private static String capitaliseFirstLetter(String choice) {
return choice.substring(0, 1).toUpperCase() choice.toLowerCase().substring(1);
}
public static String computerColorChoice() {
COLOR[] colors = COLOR.values();
return colors[new Random().nextInt(colors.length)].name();
}
enum COLOR {
Red, Green, Blue, Orange, Yellow;
}
CodePudding user response:
I'd use a List<String>
to hold the colors. This will make both user validation and computer selection much easier:
import java.util.*;
class Main {
public static Random random = new Random();
public static Scanner keyboard = new Scanner(System.in);
public static List<String> colors = Arrays.asList("RED", "GREEN", "BLUE", "ORANGE", "YELLOW");
public static void main(String[] args)
{
int correct = 0, incorrect = 0;
for(int i = 1; i <= 10; i )
{
if(userColorChoice().equalsIgnoreCase(computerColorChoice()))
{
System.out.println("You guessed correctly!");
correct ;
}
else
{
System.out.println("Sorry, your guess was wrong!");
incorrect ;
}
}
System.out.println("Amount of times you guessed correctly: " correct);
System.out.println("Amount of times you guess incorrectly: " incorrect);
}
public static String userColorChoice()
{
String userColorChoice;
do {
System.out.print("Choose either red, green, blue, orange or yellow! ");
userColorChoice = keyboard.nextLine().toUpperCase();;
} while (colors.indexOf(userColorChoice) == -1);
System.out.println("You chose: " userColorChoice);
return userColorChoice;
}
public static String computerColorChoice()
{
String randomColor = colors.get(random.nextInt(colors.size()));
System.out.println("The computer chose: " randomColor);
return randomColor;
}
}