英文:
Why won't this program 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("第二种方法变得比第一种方法更有益需要 " + totalHours2 + " 小时");
}
}
}
英文:
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 ");
}
}
}
答案1
得分: 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)
,或者您必须更改数学(计算)方法。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论