I'm making a script with a do-while loop that randomly selects a color between 6 colors. Then it asks the person to guess the color.
If they get it wrong it says Incorrect and asks if they want to try again.
If they do it selects a new color (This happens and its ok but i wish it wouldnt but thats not the problem) then they guess again.
The issue occurs when they guess the color right it still outputs saying that they're incorrect. I can't figure out why it happens and need help.
My code is the following:
import java.util.Random;
import java.util.Scanner;
public class MCassignment11 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Random rng = new Random();
String again, guess;
do {
int colorchoice = rng.nextInt(5);
String color;
if (colorchoice == 0)
color = "RED";
else if (colorchoice == 1)
color = "BLUE";
else if (colorchoice == 2)
color = "GREEN";
else if (colorchoice == 3)
color = "YELLOW";
else if (colorchoice == 4)
color = "ORANGE";
else
color = "PINK";
System.out.println("Guess my favorite color. There are 6 options.");
guess = keyboard.next();
if (guess.equals(colorchoice))
System.out.println("Correct my favorite color is " + color);
else
System.out.println("Incorrect. Would you like to try again? (y/n)" + colorchoice);
again = keyboard.next();
} while (again.equals("y"));
}
}