title: Assisstance with for loops / else-if statements in Java
body: <p>In this code I am trying to simulate a forest fire starting in the top left corner of the grid. The fire has different percentages of spreading to the next tree based on its location. The code is intended to move to the next tree and if the tree to its left is on fire it has an 85% chance to spread, and a 35% chance to spread in all other directions. For some reason my third and fourth else-if statements are not working for spreading fire to the right or above it, it essentially ignores it and I am not sure why.</p>
  <pre><code>public class ForestFire {
  
      int[][] trees = new int [30][40];
      int fire = 1;
      int no_fire = 0;
      double fire_count = 0;
      double no_fire_count = 0;
      double total_fire_count = 0;
      double total_no_fire_count = 0;
      static int total = 10;
      double average = 0;
      int trial = 0;
  
  
      
      public void FireSpread() {
          for(int x = 0; x &lt; total; x++) {
              for(int i = 0; i &lt; trees.length; i++) {         
                  for(int j = 0; j &lt; trees[i].length; j++) {
                      double random = Math.random();
                      if(i == 0 &amp;&amp; j == 0) {
                          trees[0][0] = fire;
                          System.out.print(trees[0][0] + &quot; &quot;);
                          fire_count++;
                          total_fire_count++;
                      }
                      else if((j-1) &gt;= 0 &amp;&amp; trees[i][j - 1] == fire &amp;&amp; random &lt; 0.85) {
                          trees[i][j] = fire;
                          System.out.print(trees[i][j] + &quot; &quot;);
                          fire_count++;
                          total_fire_count++;
                      }
                      else if((i-1) &gt;= 0 &amp;&amp; trees[i - 1][j] == fire &amp;&amp; random &lt; 0.35) {
                          trees[i][j] = fire;
                          System.out.print(trees[i][j] + &quot; &quot;);
                          fire_count++;
                          total_fire_count++;
                      }
                      else if((j+1) &lt;= 39 &amp;&amp; trees[i][j + 1] == fire &amp;&amp; random &lt; 0.35) {
                          trees[i][j] = fire;
                          System.out.print(trees[i][j] + &quot; &quot;);
                          fire_count++;
                          total_fire_count++;
                      }
                      else if((i+1) &lt;= 29 &amp;&amp; trees[i + 1][j] == fire &amp;&amp; random &lt; 0.35) {
                          trees[i][j] = fire;
                          System.out.print(trees[i][j] + &quot; &quot;);
                          fire_count++;
                          total_fire_count++;
                      }
                      else {
                          trees[i][j] = no_fire;
                          System.out.print(trees[i][j] + &quot; &quot;);
                          no_fire_count++;
                          total_no_fire_count++;
                      }
                      
                  }
                  System.out.println();
              }
              
              System.out.println();
              System.out.println(&quot;Fire Count: &quot; + fire_count);
              System.out.println(&quot;No Fire Count: &quot; + no_fire_count);
              System.out.println(&quot;Average: &quot; + fire_count / 1200);
              if(fire_count &gt; 480) {
                  trial ++;
              }
              average += fire_count / 1200;
              System.out.println();
              for(int i = 0; i &lt; trees.length; i++) {
                  for(int j = 0; j &lt; trees[i].length; j++) {
                      trees[i][j] = no_fire;
                  }
              }
              fire_count = 0;
              no_fire_count = 0;
          }
          System.out.println(&quot;Above 40 Percent: &quot; + trial);
          System.out.println(&quot;Total Fire Count: &quot; + total_fire_count);
          System.out.println(&quot;Total No Fire Count: &quot; + total_no_fire_count);
          System.out.println(&quot;Overall Average: &quot; + average / total);
  
          System.out.println();
  
      }
      
      public double getFireCount() {
          return fire_count;
      }
      
      public double getNoFireCount() {
          return no_fire_count;
      }
      
      public double getTotalFireCount() {
          return total_fire_count;
      }
      
      public double getTotalNoFireCount() {
          return total_no_fire_count;
      }
      
      public static void main(String[] args) {
          ForestFire one = new ForestFire();
          one.FireSpread();
          System.out.println(&quot;Average Fire Count: &quot; + one.getTotalFireCount() / total);
          System.out.println(&quot;Average No Fire Count: &quot; + one.getTotalNoFireCount() / total);
  
      }
  }
  </code></pre>