title: Trying to make a value in a loop constant based on a given condition
body: <p>I am working on an exercise and I thought I was done until I realized that some of my results were wrong.</p>
  <p>This is the function I called to get the result:</p>
  <pre><code>System.out.println(chargingSmartPhone(10, 50));
  </code></pre>
  <p>It is expected to return 7. I got 7 but the calculation is not right.</p>
  <p>I am expected to follow this instruction:</p>
  <p>S = 10, T = 50. In this condition R = 10 applies, and charge becomes 10 + 10 = 20.</p>
  <p>Now charge rate changes to R = 5, then after 6 more minutes charge becomes 20+(5+5+5+5+5+5) = 50</p>
  <p>Therefore total time: 7 minutes.</p>
  <p>THis means I should have</p>
  <p>10 + 10</p>
  <p>20 &lt;= 10</p>
  <p>==========</p>
  <p>20 + 5</p>
  <p>25 &lt;= 230</p>
  <p>==========</p>
  <p>25 + 5</p>
  <p>30 &lt;= 230</p>
  <p>==========</p>
  <p>30 + 5</p>
  <p>35 &lt;= 230</p>
  <p>==========</p>
  <p>35 + 5</p>
  <p>40 &lt;= 230</p>
  <p>==========</p>
  <p>40 + 5</p>
  <p>45 &lt;= 230</p>
  <p>==========</p>
  <p>45 + 5</p>
  <p>50 &lt;= 230</p>
  <p>==========</p>
  <p>7</p>
  <p>But instead, I have this result:</p>
  <p>10 + 10</p>
  <p>20 &lt;= 10</p>
  <p>==========</p>
  <p>20 + 5</p>
  <p>25 &lt;= 230</p>
  <p>==========</p>
  <p>25 + 8</p>
  <p>33 &lt;= 559</p>
  <p>==========</p>
  <p>33 + 2</p>
  <p>35 &lt;= 1009</p>
  <p>==========</p>
  <p>35 + 7</p>
  <p>42 &lt;= 5000</p>
  <p>==========</p>
  <p>42 + 8</p>
  <p>50 &lt;= 10000</p>
  <p>==========</p>
  <p>50 + 3</p>
  <p>53 &lt;= 1000000000</p>
  <p>==========</p>
  <p>7</p>
  <p>I have to terminate the program when the intialCharge += r[i] is greater than final which is working fine.</p>
  <p>Below is my code:</p>
  <pre><code>public static int chargingSmartPhone(int initialCharge, int finalCharge){
          int count = 0;
          int[] v = {10, 230, 559, 1009, 5000, 10000, 1000000000};
          int[] r = {10, 5, 8, 2, 7, 8, 3};
          for(int i = 0; i &lt; v.length; i++){
              if(initialCharge &lt;= finalCharge) { // InitialCharge is S, FinalCharge is T
                  if (initialCharge &lt;= v[i]) {
                      System.out.println(initialCharge + &quot; + &quot; + r[i]);
                      initialCharge += r[i];
                      System.out.println(initialCharge +&quot; &lt;= &quot;+ v[i]);
                      System.out.println(&quot;==========&quot;);
                      count++;
                  }
              }
          }
          return count;
      }
      public static void main(String[] args){
          System.out.println(chargingSmartPhone(10, 50));
      }
  </code></pre>
  <p>What am I doing wrong?</p>