我采用了for循环,根据两个因素计算每个月的工资:固定工资50000美元和一个月额外工作时间550美元/小时。后一个变量显然每月变化,因此我采用扫描仪类来接收每个月的输入。 (我的循环之前的所有必需参数都已经充分定义)我的循环看起来像这样:
for(int month = 1; month <= 12; month++){
System.out.print("How many extra hours did you work this month?");
double extraHoursPerMonth = scan.nextInt();
double bonusSalary = extraHoursPerMonth*bonusSalaryPerHour;
double totalMonthlySalary = basicSalary + bonusSalary;
System.out.println("Your salary for this month is $" + totalMonthlySalary);
运行时,会成功计算每个月的总薪水。那怎么去找年薪呢?
我找不到任何我可以使用的代码,总结一下,以前计算成最终总和的月工资,我觉得我对抗了一堵砖墙。我将不胜感激任何有关如何前进的指针,提示或建议。
您必须在for循环之外创建一个变量,并为每次迭代添加月薪,例如
int annualSalary = 0;
for(int month = 1; month <= 12; month++){
System.out.print("How many extra hours did you work this month?");
double extraHoursPerMonth = scan.nextInt();
double bonusSalary = extraHoursPerMonth*bonusSalaryPerHour;
double totalMonthlySalary = basicSalary + bonusSalary;
System.out.println("Your salary for this month is $" +
totalMonthlySalary);
}
System.out.println("Your annul salary for this year is $" + annualSalary);
如果你想要年薪,你可以通过将月薪乘以12得到它。
double anualSalary = basicSalary*12;
如果你想要整体薪水,你应该在声明anualSalary之后和for循环之前声明一个变量。
double overalSalary = anualSalary;
然后在for循环的每个循环中添加奖金工资。
...
overalSalary += bonusSalay;
...