0

I am trying to write a simple command line "word guessing game" with an Array of colors. The problem is that it won't break out of the do/while loop with my current IF statement even when the correct color is chosen.

import java.util.Random;
import java.util.Scanner;

public class ColorGuess {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);     

        String[] colors = { "red", "blue", "green", "yellow", "purple",
                "pink", "white", "black", "brown", "orange", "grey" };

        String colorGuess;

        Random rand = new Random();
        int randColor = rand.nextInt(colors.length);
        String color = colors[randColor];

        System.out.printf("Guess the color: ");

        do {
            colorGuess = input.nextLine();
            if (color == colorGuess) {
                break;
            } else {
                System.out.printf("Sorry, try again: ");
            }
        } while (colorGuess != color);

        System.out.println("You got it! The color was indeed " + color + ".");

    }

}
Mos Def
  • 11
  • 1
  • 6

0 Answers0