首页 > 解决方案 > 初学者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");
                    }

程序编译,现在我想添加一些额外的输出,我需要一些“是”的答案。

如何添加一个计数器来跟踪“是”答案的数量。?

循环完成后,我需要回答“是”的总数,以便计算总的健康保险费用。

感谢您的见解

标签: javastringif-statementcounter

解决方案


添加一个计数器变量,如下所示:

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的用户可以回答“是”、“否”或“可能”的情况。


推荐阅读