java中如何将循环内计算的月薪汇总为年薪

我采用了 for 循环,根据两个因素计算每个月的工资:50000 美元的固定工资和每月额外工作 550 美元/小时的额外工资。后一个变量显然每个月都在变化,所以我采用了scanner类来接收每个月的输入。(循环之前的所有必需参数都已充分定义)我的循环如下所示:


    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);

运行时,成功计算每个月的总工资。那么我如何继续查找年薪?


我找不到任何我可以使用的代码来总结以前计算到最终金额的月薪,我觉得我遇到了砖墙。我将不胜感激关于如何前进的任何指示、提示或建议。


幕布斯6054654
浏览 244回答 2
2回答

万千封印

您必须在 for 循环之外创建一个变量,并将每次迭代的月薪相加,例如int annualSalary = 0;&nbsp; for(int month = 1; month <= 12; month++){&nbsp; &nbsp; System.out.print("How many extra hours did you work this month?");&nbsp; &nbsp; double extraHoursPerMonth = scan.nextInt();&nbsp; &nbsp; double bonusSalary = extraHoursPerMonth*bonusSalaryPerHour;&nbsp; &nbsp; double totalMonthlySalary = basicSalary + bonusSalary;&nbsp; &nbsp; System.out.println("Your salary for this month is $" +&nbsp;&nbsp; &nbsp; totalMonthlySalary);&nbsp; }&nbsp; System.out.println("Your annul salary for this year is $" + annualSalary);

杨魅力

如果你想要年薪,你可以通过将月薪乘以 12 来获得。double anualSalary = basicSalary*12;如果你想要整体工资,你应该在anualSalary声明之后和 for 循环之前声明一个变量。double overalSalary = anualSalary;然后在 for 循环的每个循环中添加奖金工资。...overalSalary += bonusSalay;...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java