Gia
Gia

Reputation: 45

Why won't this program print?

The problem is to determine the number of hours required before the second method becomes more beneficial than the first. I've been looking at this all day and don't know why I can't get it to print.

 public class BestMethod{
    
      public static void main(String[] args){
    
    double earnings1 = 10.00;
    double earnings2 = 0.10;
    double totalHours1 = 0;
    double totalHours2 = 0;
    double x = 0;
    double y = 0;
    
    
    for (int i = 1; i <= 10; i++)
    
    {
      totalHours1++;
        x = totalHours1 * earnings1;
    
      totalHours2++;
        earnings2 *= 1;
          y = (earnings2 * 1) * 2 + earnings2;
        }
    
      if (y > x){
        System.out.println ("It will take the second method " + totalHours2 + " hours before it becomes more beneficial than the first method ");
       }
    
     }
    }

Upvotes: 0

Views: 42

Answers (1)

mostafiz67
mostafiz67

Reputation: 169

Based on your code, the value of earnings1 and earnings2 are remaining the same.

After the first iteration (i = 1) how values are changing (use pen and paper)

totalHours1 = 1.0, totalHours2 = 1.0, x = 10.0 and y = 0.30000000000000004

After completing the loop (i = 10)

totalHours1 = 10.0, totalHours2 = 10.0, x = 100.0 and y = 0.30000000000000004

So, the value of y is less than the value of x and the condition is going to false.

So, (in case if you want to) print the value use this in your program if (y < x) or you have to change the mathematical (calculation) method.

Upvotes: 1

Related Questions