问题在于确定第二种方法比第一种方法更有利之前所需的小时数。我整天都在看这本书,不知道为什么我无法打印它。
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 ");
}
}
}
答案 0 :(得分:1)
根据您的代码,earnings1
和earnings2
的值保持不变。
(i = 1)
第一次迭代后,值如何变化(使用笔和纸)
totalHours1 = 1.0
,totalHours2 = 1.0
,x = 10.0
和y = 0.30000000000000004
完成循环(i = 10)
totalHours1 = 10.0
,totalHours2 = 10.0
,x = 100.0
和y = 0.30000000000000004
因此,y
的值小于x
的值,条件将变为false
。
因此,(如果需要的话)打印值,请在程序if (y < x)
中使用此值,否则必须更改数学(计算)方法。