0

The whole code is working as it's supposed to except the last line where it should allow user to enter the choice. Instead it never allows the user to enter the choice and terminates the whole program. I need the it to let the user decide whether they want to run the code again. Help would be appreciated a lot.

import java.lang.Math;
public class Game {
public static void main(String[] args) {
    // declare and instantiate a Scanner
    Scanner in = new Scanner(System.in);

     // declare and initialize variables
     int i = 0, guess = 0, g1, g2, g3, r1, r2, r3, randnum;
     String choice = "yes";
     while (choice.equals("yes")) {
         randnum = (int) (Math.random() * 100 + 100);
         System.out.println("----- MASTERMIND -----");
             while (guess != randnum) {
                 System.out.println("Guess the 3 digit number!");
                 System.out.println(randnum);
                 System.out.println("Guess " + ++i + ": ");
                 guess = in.nextInt();
                 int correctdig = 0;
                 r1 = randnum % 10;
                 g1 = guess % 10;
                 if (g1 == r1) {
                     correctdig++;
                 }
                 r2 = randnum / 10;
                 g2 = guess / 10;

                 r1 = r2 % 10;
                 g1 = g2 % 10;
                 if (g1 == r1) {
                     correctdig++;
                 }
                 r3 = r2 / 10;
                 g3 = g2 / 10;

                 if (g3 == r3) {
                     correctdig++;
                 }
                 System.out.println("You matched " + correctdig);
             }
         }
         System.out.println("Congratulations! You guessed the right number in " + i + " guesses");
         System.out.println("Would you like to play again (yes/no)?");
         choice = in.nextLine();
     }
 }```

0 Answers0