很抱歉,我对自己的头衔感到困惑,但是我是编程的极端初学者。我的家庭作业是显示一个人每天加倍工作的天数(第1天:$ 1.00,第2天:$ 2.00,第3天:$ 4.00等)。我必须显示他们每天所赚的薪水以及他们每天所赚的总薪水。
我无法弄清楚我如何获得所有工作日中所赚取的全部工资之和。救命?
for ( int daysWorked = 1; daysWorked <= numDaysWorked; daysWorked++) {
System.out.println("Pay for day #" + daysWorked + ": " + formatter.format(amount));
amount = daysWorked * 2;
totalPay = ?? ;
}
System.out.println("\nTOTAL PAY FOR " + numDaysWorked + " DAYS: " + formatter.format(totalPay));
最佳答案
double amount = 1; // make sure it initially equals 1
for (int daysWorked = 1; daysWorked <= numDaysWorked; daysWorked++) {
System.out.println("Pay for day #" + daysWorked + ": " + formatter.format(amount));
totalPay += amount; // add the amount value each time to the totalPay variable
amount *= 2; // it doubles so just double it every loop
}
System.out.println("\nTOTAL PAY FOR " + numDaysWorked + " DAYS: " + formatter.format(totalPay));