为什么该程序无法打印?

时间:2020-09-20 20:27:24

标签: java for-loop printing

问题在于确定第二种方法比第一种方法更有利之前所需的小时数。我整天都在看这本书,不知道为什么我无法打印它。

 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 ");
       }
    
     }
    }

1 个答案:

答案 0 :(得分:1)

根据您的代码,earnings1earnings2的值保持不变。

(i = 1)第一次迭代后,值如何变化(使用笔和纸)

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

完成循环(i = 10)

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

因此,y的值小于x的值,条件将变为false

因此,(如果需要的话)打印值,请在程序if (y < x)中使用此值,否则必须更改数学(计算)方法。