title: Nested Loop not working properly in Mastermind game
body: <p>I recently started coding in Java and my coach gave me an exercise where I have to re-create the Mastermind game. To give an overview of this game: the computer creates an array with X random integers, and the user gets to input X integers as well. Location matters. The users scores "Gold" if he guesses an integer that is in the same spot as the computer generated array. If the integer is present in the array, but in the wrong spot, the users gets a "Silver" score. If the integer is not present in the array at all, the user gets a "NotFound" score. The final array should give the user a score for each place in the array, e.g. (Gold, Silver, NotFound)</p>
  
  <p>I have tried to make a nested loop that scores the users guesses. It captures the score in a different array (yourScore[]). User guesses are captured in array "guessednums[]" and the computer generated array is called "nums[]". The size of all arrays has been set with a variable prior to the mentioned code.</p>
  
  <p>What I want the code to do is to first check whether a user's guess matches with the computer choice on the same spot, and if that is the case set the matching space in the yourScore array to "Gold". Then, if the guess does not directly match, I want a loop to check whether the user guess is present in ANY place of the computer generated nums[] array, and to score it as "Silver" if that is the case. Finally, if this is not the case, I want the yourScore[] place to be set as "NotFound".</p>
  
  <p>Matching guesses are properly scored as "Gold". The problem I am encountering is that sometimes the loop will not properly score a guess as "Silver", but instead mark it as "NotFound".  I suspect that this has to do with the loop not properly initialising. I have read multiple questions on Stack Overflow and other articles as well, and played around with my code, but I keep running into similar issues. </p>
  
  <p>I would love to get a second opinion on the code and see what I have done wrong.</p>
  
  <pre><code>package Mastermind;
  
  import java.util.Arrays;
  import java.util.Random;
  import java.util.Scanner; 
  
  public class Mastermind {
      static Scanner userInput = new Scanner(System.in);
  
      public static void main(String[] args){
  
          int size = 3; // Allows you to set the size of the arrays in the game
  
          int[] nums = generateNumbers(size); //Stores the computer choice 3-tuple 
          int[] guessednums; //Stores the guessed 3-tuple
          int iteration = 0; //Keeps track of the # of guesses so far
          boolean correctAnswer; //true if the guessed tuple matches the computer choice tuple
  
          //Set array size
  
  
          //The game starts here
          while (true){
              iteration++;
              System.out.println("Iteration #" + iteration);
              guessednums = guessTheNumbers(size);
              correctAnswer = yourScore(nums, guessednums, size);
              if(correctAnswer) break;
          }
  
          //Printing the result
          printResults(iteration, nums);
  
      }
  
      private static void printResults(int iteration, int[] nums) {
          System.out.println("************************************************************"); // Print final result if users has a completely matching guess
          System.out.println("************************************************************");
          System.out.println("The correct answer was " + Arrays.toString(nums));
          System.out.println("It took you " + iteration + " iterations to find the answer!");
      }
  
      private static int[] guessTheNumbers(int size) {
          System.out.println("Please your ordered guess (press enter after each):");
          int[] guessednums = new int[size]; // Initialise array for user choices
  
          for (int i = 0; i &lt; size; i++){ // Loop that creates the array of user input
              guessednums[i] = userInput.nextInt();
          }
  
          System.out.println(Arrays.toString(guessednums));
          return guessednums; // Return array for user guessed numbers array to main method
      }
  
      public static int[] generateNumbers(int size){
          int[] nums = new int[size]; // Initialise array for computer choices
          Random rn = new Random(); // Create new variable for randomised computer choices
  
          for (int i = 0; i &lt; size; i++){ // Loop that creates the array of computer choices
              nums[i] = rn.nextInt(9) + 1;
          }
  
          System.out.println(Arrays.toString(nums)); // Temporary to print array
          return nums; // Return array for computer generated numbers array to main method
  
      }
  
      public static boolean yourScore(int[] nums, int[] guessednums, int size){   
          String[] yourScore = new String[size]; // Initialise array for user choices
  
          for(int i = 0; i &lt; size;  i++){ // Nested loop that scores the user entries as Gold/Silver/NotFound
              if (guessednums[i] == nums[i]){
                  yourScore[i] = "Gold";
              } else {
                  yourScore[i] = "NotFound";// in case is not found it stays that way
                  for(int j = 0; j &lt; size;  j++){
                      if (guessednums[i] == nums[j]){
                          yourScore[i] = "Silver"; // found one! break the loop and keep checking numbers
                          break;
                      } 
                  }
              }
          }
  
  
          if (yourScore[0] == "Gold" &amp;&amp; yourScore[1] == "Gold" &amp;&amp; yourScore[2] == "Gold"){ // Marks the input as true or false depending on if it matches with the computer choices
              boolean correctanswer = true;
              return correctanswer;
          } else {        
              System.out.println("Your score is " + Arrays.toString(yourScore) + "!");
              System.out.println("************************************************************");
              boolean correctanswer = false;
              return correctanswer;
          }
      }
  }
  </code></pre>