这个程序为什么不会打印?

huangapple go评论118阅读模式
英文:

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 &lt;= 10; i++)
    
    {
      totalHours1++;
        x = totalHours1 * earnings1;
    
      totalHours2++;
        earnings2 *= 1;
          y = (earnings2 * 1) * 2 + earnings2;
        }
    
      if (y &gt; x){
        System.out.println (&quot;It will take the second method &quot; + totalHours2 + &quot; hours before it becomes more beneficial than the first method &quot;);
       }
    
     }
    }

答案1

得分: 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 &lt; 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 &lt; x) or you have to change the mathematical (calculation) method.

huangapple
  • 本文由 发表于 2020年9月21日 04:27:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63983339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定