初学者 Java 薪资程序,带有嵌套 if else if 控件的 While 循环,需要计算有多少 "yes" 个答案

Beginner Java Payroll Program, While loop with nested if else if controls, need to count how many "yes" answers

正在开发一个工资单初学者程序,其中包含由员工人数控制的 while 循环。

在 while 循环中,员工按薪水排序。

"if" 工资 < 85,000 小时的员工有资格加班。

"else"(工资 > 85)该员工 "salaried employee" 不符合加班条件。

在代码的 "if" 和 "else" 分支中,有嵌套的 if/else/if else 控制税级、加班、带薪休假等。

在 "if" 和 "else" 分支机构中,我询问员工是否有公司健康保险,以便我可以从员工工资中扣除保险费用。

这是我的代码,用于询问员工是否有公司健康保险,YES/NO?


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 0 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");
                    }

程序编译,现在我想添加一些额外的输出,我需要 "Yes" 个答案。

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

循环结束后,我需要回答“是”的总数,以便计算医疗保险总费用。

谢谢您的见解

像这样添加一个计数器变量:

int yes;

if (yesORno.equalsIgnoreCase("YES")) {
    yes++;

    // the rest of your TRUE code here
}
else {
    // the rest of your FALSE code here
}

确保在代码分支处递增 yes (yes++) 以处理来自用户的 "yes" 回答。

注意:当您的条件只有两个结果时,您不需要使用 else if。一个简单的 else 就足够了。为用户可以回答 "yes"、"no" 或 "maybe".

的情况保存您的 else if