如何在循环中的特定点创建一个在不停止循环的情况下触发的输出?

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

How to create an output that triggers at a certain point in a loop without stopping the loop?

问题

以下是翻译好的部分:

public class IncreasedProduction {

    public static void main(String[] args) {
        // 声明变量

        double rate = 0.06;
        double production = 4;

        // 列标题
        System.out.println("(以千为单位测量的零件产量)\n\n" + "月份" + "   " + "产量\n");

        // 计算每个迭代的次数
        for (int month = 1; month > 0; month++) {
            production = production * Math.pow(1.0 + rate, month);

            // 显示每月总产量
            if (month < 25) {
                // 显示每月总数
                System.out.println("  " + month + "      " + String.format("%.2f", production) + "\n");

                // 计算首次生产7000个零件的时间并显示消息
                if (production >= 7 && (production / 1.06) < 7) {
                    System.out.println(">>>  你在第 " + month + " 个月生产了7000个零件,你应该获得加薪。 <<<\n");
                }
            } else {
                // 在24个月结束时显示月份和总产量
                System.out.println("\n在 " + month + " 个月内,你生产了 " + String.format("%.2f", production) + " 个零件!");
                // 退出循环
                month = -1;
            }
        }
    }
}

请注意,翻译是基于您提供的源代码进行的,我只对代码进行了翻译,没有包含其他额外的内容。

英文:

I'm trying to sort out the correct code to an assignment I got back from an OOJ class, we are using Java.

I'm struggling with two issues. First, I need the program to calculate compounding growth using the rate provided (6%) and print to console each month's production with each iteration over the course of 24 months. Second, I need to display the month when production passes 7000.I was told by my professor production should pass 7000 on month 10, mine is showing it sooner.

Something is off in my calculation and I'm not sure what I'm overlooking. I'd appreciate any input and general advice on how to improve the code.

>The Freemont Automobile Factory has discovered that the longer a worker has been on the job,
the more parts the worker can produce. Write an application that computes and displays
a worker’s anticipated output each month for 24 months assuming the worker starts by producing
4,000 parts and increases production by 6 percent each month. Also display the month in which
production exceeds 7,000 parts (when the worker deserves a raise!).

public class IncreasedProduction {

	public static void main(String[] args) {
		//declare variables
		
		double rate = .06;
		double production = 4;
		
		//column titles
		System.out.println(&quot;(Parts produced measured in thousands)\n\n&quot;+ &quot;Month&quot; + &quot;   &quot; + &quot;Parts Produced\n&quot;);
		
			//counts the number of each iterations
			for(int month = 1; month &gt; 0; month++)
			{	
				production = production*Math.pow(1.0 + rate, month);								
				
				//displays the monthly total parts produced
				if( month &lt; 25 )
				{
					//Displays monthly total
					System.out.println(&quot;  &quot; + month + &quot;      &quot; + String.format(&quot;%.2f&quot;,production) + &quot;\n&quot;);
					
					//calculates the moment when 7000 parts are first produced and displays a message
					if( production &gt;= 7 &amp;&amp; (production/1.06) &lt; 7) {
						System.out.println(&quot;&gt;&gt;&gt;  You produced 7000 parts in &quot; + month + &quot; months, you deserve a raise. &lt;&lt;&lt;\n&quot;);
				}}
				else
				{	//displays number of month and total parts produced at the end of 24 months
					
					System.out.println(&quot;\nIn &quot; + month + &quot; months you produced &quot; + String.format(&quot;%.2f&quot;,production) + &quot; parts!&quot;);
					//break
					month = -1;
				
				}
			}		
	}
}

答案1

得分: 1

计算生产增加似乎不正确:

production = production*Math.pow(1.0 + rate, month);

因为它将增长乘了两次:

  1. K = (1 + r)^month
  2. production = production * K

因此,添加增量和增加计算到输出后,每月的生产增加在第一个月后显着增长:

delta=240 -> 6.00%    1      4240.00
delta=524 -> 12.36%    2      4764.06
delta=910 -> 19.10%    3      5674.08
delta=1489 -> 26.25%    4      7163.39
>>>  您在4个月内生产了7000件零件,您应该加薪。 <<<

通过修正仅计算一次增加来解决此问题:

production = Math.round(production*(1.0 + rate));
// 或者等效的
// production = Math.round(4000*Math.pow(1.0 + rate, month));

输出

月份   生产的零件数

  1      4240.00
  2      4494.00
  3      4764.00
  4      5050.00
  5      5353.00
  6      5674.00
  7      6014.00
  8      6375.00
  9      6758.00
  10      7163.00

>>>  您在10个月内生产了7000件零件,您应该加薪。 <<<
  
  11      7593.00
英文:

Calculation of the increase of the production does not seem to be correct:

production = production*Math.pow(1.0 + rate, month);

as it is multiplying the increase twice:

  1. K = (1 + r)^month
  2. production = production * K

So the monthly production increase grows significantly after the first month, which is evident after adding the delta and increase calculation to the output:

delta=240 -&gt; 6.00%    1      4240.00
delta=524 -&gt; 12.36%    2      4764.06
delta=910 -&gt; 19.10%    3      5674.08
delta=1489 -&gt; 26.25%    4      7163.39
&gt;&gt;&gt;  You produced 7000 parts in 4 months, you deserve a raise. &lt;&lt;&lt;

This is resolved by correction to count the increase only one time:

 production = Math.round(production*(1.0 + rate));
// or equivalent
// production = Math.round(4000*Math.pow(1.0 + rate, month));

output

Month   Parts Produced

  1      4240.00
  2      4494.00
  3      4764.00
  4      5050.00
  5      5353.00
  6      5674.00
  7      6014.00
  8      6375.00
  9      6758.00
  10      7163.00

&gt;&gt;&gt;  You produced 7000 parts in 10 months, you deserve a raise. &lt;&lt;&lt;

  11      7593.00

huangapple
  • 本文由 发表于 2020年10月17日 06:40:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64397296.html
匿名

发表评论

匿名网友

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

确定