-3

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"));
    }
}
dunni
  • 43,386
  • 10
  • 104
  • 99

2 Answers2

3

One issue here is that your comparison is case-sensitive, meaning that if the user entered something like Red, red, or rEd or something like that it'll be considered incorrect. (Technically, Red and RED aren't equal strings).

One option is to use the toUpperCase() method to make sure that the user input will be treated as if they wrote it with capital letters, like this:

if (guess.toUpperCase().equals(color))

As @OHGODSPIDERS pointed out in the comments, Java strings also have an equalsIgnoresCase() method that you could use here too. Either will work.

Also, as @Alessandro pointed out, you should compare guess to color rather than to colorchoice.

One other point:

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();

To make sure that you don't prompt the user to keep guessing, you should do the following:

if (guess.toUpperCase().equals(color))
{
            System.out.println("Correct my favorite color is " + color);
            // Explicitly set "again" to "n" so that we won't loop again
            again = "n";
}

else
// Add brackets so that it only prompts the user for input if they had a wrong answer
{
            System.out.println("Incorrect. Would you like to try again? (y/n)" + colorchoice);
        again = keyboard.next();
}
0

You are comparing String to int:

if (guess.equals(colorchoice))

So you should convert one type to the other before.

You could convert the string using Integer.parseInt() or the int concatenating it to "".

See following please:

public static void main(String[] args) {
    String a = "1";
    int b = 1;
    System.out.println(a.equals(b)); //Prints false
    System.out.println(a.equals("" + b)); //Prints true
    System.out.println(Integer.parseInt(a)==b); //Prints true
}
Alessandro
  • 4,382
  • 8
  • 36
  • 70