title: How can I turn this into a for loop?
body: <p>I am writing a Mastermind program in Java. So the user has to enter the number of pegs and colors and a code will randomly be generated and  the program will do its thing and tell them if they got the code or not. I have a bunch of IF statements and I tried putting it into a for loop so I don't have to create a if statement when the user increases the amt of pegs but that does not work. Any suggestion? Here is the code:</p>
  <pre><code>public void program(int [] peg, int amtpegs,int amtcolors) {
      Random generator = new Random();
      KeyboardReader reader = new KeyboardReader();
      int color = 0, guess = 1,pegs = 0;
      int [] guesses = new int[10];
      for(int i = 0; i&lt;peg.length;i++) {
          
          
          
          peg[i]=generator.nextInt(amtpegs)+1;
          System.out.println(peg[i]);
      }
            //Repeat till the player wins
            while(true){
              //Get input from user
                for(int x = 0; x&lt;amtpegs;x++)
                  {
                    guesses[x]=reader.readInt(&quot;Enter your numbers: &quot;);
                  
                  }
              //Check if peg in correct index
             for(int y = 0; y&lt;amtpegs;y++) {
              if (peg[y]==guesses[y]){
                pegs++;
              
             }
              //Check if the color is correct
              if (guesses[0]==peg[0] || guesses[0]==peg[1] || guesses[0]==peg[2]){
                color++;
              }
              if (guesses[1]==peg[1] || guesses[1]==peg[0] || guesses[1]==peg[2]){
                color++;
              }
              if (guesses[2]==peg[0] || guesses[2]==peg[1] || guesses[2]==peg[2]){
                color++;
              }
              if (guesses[3]==peg[3] || guesses[3]==peg[1] || guesses[3]==peg[2]){
                    color++;
              }
                  
              System.out.println(&quot;You have &quot;+pegs+&quot; correct peg(s) and &quot;+color+&quot; correct colors&quot;);
              //Exit if player wins
              if (color==amtcolors &amp;&amp; pegs==amtpegs){
                System.out.println(&quot;You have broken the code in &quot;+guess+&quot; guesses&quot;);
                break;
              }
              //Increment guess count
              guess++;
              //Reset the color and peg value
              color=0;
              pegs=0;
              
            
            }
  </code></pre>
  <p>i tried making the if or statements into a for loop by replacing the indexes with the x and j in the for loop:</p>
  <pre><code>for(int x = 0; x&lt;amtpegs;x++) {
                // System.out.println(x+&quot;n&quot;);
                 for(int j = 0; j&lt;amtpegs; j++){
                  //   System.out.println(j+&quot;N&quot;);
                   if (guesses[x]==peg[j]){
                     color++;
                     if (color==amtcolors &amp;&amp; pegs==amtpegs){
                        System.out.println(&quot;You have broken the code in &quot;+guess+&quot; guesses&quot;);
                        break;
                      }
                   }
  </code></pre>
  <p>but that does not work since my variable colors increases in values. EX: If i input 10 pegs and 9 colors it will come out a 10 pegs and 20 or 30 colors.</p>