Please help. My do while loop is acting opposite of what it is supposed to do. When I change the condition to while(play == "yes") it exists regardless of what input. And with (play != "no") it loops regardless of what I input. I think it is the scan.nextLine() portion but I dont know how to fix it..
import java.util.Scanner;
import java.util.Random;
public class Assignment4 {
public static void main(String [] args)
{
System.out.println("----- MASTERMIND -----");
Random rand = new Random();
String play = " ";
Scanner scan;
scan = new Scanner(System.in);
do
{
int guess = 0;
int secretNum = rand.nextInt(9999);
System.out.println(secretNum);
System.out.println("Guess the 4 digit number!");
int first = 0, second = 0, third = 0, fourth = 0;
int i = 1;
while (guess != secretNum)
{
System.out.println("Guess " + i + ": " );
guess = scan.nextInt();
if (String.valueOf(guess).charAt(0) == String.valueOf(secretNum).charAt(0))
{
first = 1;
}
if (String.valueOf(guess).charAt(1) == String.valueOf(secretNum).charAt(1))
{
second = 1;
}
if (String.valueOf(guess).charAt(2) == String.valueOf(secretNum).charAt(2))
{
third = 1;
}
if(String.valueOf(guess).charAt(3) == String.valueOf(secretNum).charAt(3))
{
fourth = 1;
}
int total = (first + second + third + fourth);
System.out.println("You matched " + total);
if (guess != secretNum)
{
first = 0;
second = 0;
third = 0;
fourth = 0;
}
i++;
}
System.out.println("Congratulations! You guessed the right number in " + (i-1) + " guesses.");
scan.nextLine();
System.out.println("Would you like to play again (yes/no)? ");
play = scan.nextLine();
} while (play != "no");
System.out.print("Bye!");
}
}