英文:
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("(Parts produced measured in thousands)\n\n"+ "Month" + " " + "Parts Produced\n");
//counts the number of each iterations
for(int month = 1; month > 0; month++)
{
production = production*Math.pow(1.0 + rate, month);
//displays the monthly total parts produced
if( month < 25 )
{
//Displays monthly total
System.out.println(" " + month + " " + String.format("%.2f",production) + "\n");
//calculates the moment when 7000 parts are first produced and displays a message
if( production >= 7 && (production/1.06) < 7) {
System.out.println(">>> You produced 7000 parts in " + month + " months, you deserve a raise. <<<\n");
}}
else
{ //displays number of month and total parts produced at the end of 24 months
System.out.println("\nIn " + month + " months you produced " + String.format("%.2f",production) + " parts!");
//break
month = -1;
}
}
}
}
答案1
得分: 1
计算生产增加似乎不正确:
production = production*Math.pow(1.0 + rate, month);
因为它将增长乘了两次:
- K = (1 + r)^month
- 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:
- K = (1 + r)^month
- 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 -> 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
>>> You produced 7000 parts in 4 months, you deserve a raise. <<<
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
>>> You produced 7000 parts in 10 months, you deserve a raise. <<<
11 7593.00
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论