java - 初学者Java工资单程序,带有嵌套if else if控件的While循环,需要计算有多少“是”答案
问题描述
使用由员工数量控制的 while 循环来处理初学者工资单程序。
在 while 循环中,员工按薪水排序。
“如果”工资 < 85K 员工按小时支付并且有资格加班。
"else" (salary > 85) 员工是“受薪员工”,不符合加班条件。
在代码的“if”和“else”分支中都有嵌套的 if/else/if else 控件,用于控制税级、加班、带薪休假等。
在“if”和“else”两个分支中,我询问员工是否有公司健康保险,以便我可以从员工工资中扣除保险费用。
这是我询问员工是否有公司健康保险的代码,是/否?
System.out.println("Does " + empName + " have company health insurance? Enter YES or NO.");
yesORno = input.next();
if (yesORno.equalsIgnoreCase("YES")) {
System.out.println(empName + " Has company health insurance and pays $300 each pay period.");
empAfterInsuranceAnnual = empAnnualNet - 7800;
empAfterInsurancePP = empPayPeriodNet - 300;
System.out.println(empName + " Net annual income after taxes and health insurance is $" + empAfterInsuranceAnnual +".");
System.out.println(empName + " Net pay period income after taxes and health insurance is $" + empAfterInsurancePP + ".");
}
else if (yesORno.equalsIgnoreCase("NO")) {
System.out.println(empName + " does not have company health insurance payroll deduction");
}
程序编译,现在我想添加一些额外的输出,我需要一些“是”的答案。
如何添加一个计数器来跟踪“是”答案的数量。?
循环完成后,我需要回答“是”的总数,以便计算总的健康保险费用。
感谢您的见解
解决方案
添加一个计数器变量,如下所示:
int yes;
if (yesORno.equalsIgnoreCase("YES")) {
yes++;
// the rest of your TRUE code here
}
else {
// the rest of your FALSE code here
}
确保yes++
在您的代码分支处增加 yes ( ) 以处理来自用户的“是”回答。
else if
注意:当您的条件只有两个结果时,您不需要使用。一个简单的else
就足够了。保存您else if
的用户可以回答“是”、“否”或“可能”的情况。
推荐阅读
- mysql - 如何根据tableB记录行在tableA中插入多条记录?
- python - 用python配置文件
- amazon-web-services - 尝试从私有 EC2 连接时,海王星抛出“主机没有及时响应”
- c# - 生成“Aspnet-api-versioning”时 Swagger 失败
- angularjs - BigCommerce 结帐上的 Angular JS
- angular - 使用 Angular 2+ 在同一页面中显示各种视图时面临问题
- bash - Bash:遍历每个子文件夹中的每个文件并重命名
- html - 如何使用 excel vba 从网站中提取特定数据(名称、详细信息)到 excel 中?
- php - 显示适用于 java 目录中单个页面的所有类别/术语
- powershell - 如何提取进程(对象)的 CPU 百分比值?