You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何判断switch case选中分支并创建对应类型的员工对象

问题修复方案

核心问题说明

你现有代码的问题出在switch(count)逻辑和用户选择的employeeType完全无关,只是按循环下标固定实例化对应类型的员工,自然和用户输入不匹配。

修复逻辑

直接删除原有switch(count)代码块,替换为根据用户选择的employeeType创建对应类型对象,直接赋值给当前循环下标对应的数组位置即可。同时补充非法输入的跳过逻辑,避免数组存入无效null值。

修改后代码片段

for (count = 0; count < 5; ) { // count自增移到合法输入之后,避免非法输入占用数组位置
     employeeTypeString = JOptionPane.showInputDialog(null, "What type of employee are you?\n\nEnter the number that corresponds to your answer.\n1. Salaried\n2. Hourly\n3. Commissioned\n4. Base Plus Commissioned", "Employee Type", JOptionPane.QUESTION_MESSAGE);
     employeeType = Integer.parseInt(employeeTypeString);

    // 所有员工通用的姓名、SSN采集提到分支外,减少重复代码
    firstName = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
    lastName = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
    ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);

     switch (employeeType) {
        case 1:
           salaryString = JOptionPane.showInputDialog(null, "What is your weekly salary?", "Salary", JOptionPane.QUESTION_MESSAGE);
           salary = Double.parseDouble(salaryString);
           // 直接创建对象赋值给当前数组位置
           employees[count] = new SalariedEmployee(firstName, lastName, ssn, salary);
           count++; // 合法输入才自增计数
           break;

        case 2:
           wageString = JOptionPane.showInputDialog(null, "What is your hourly wage?", "Hourly Wage", JOptionPane.QUESTION_MESSAGE);
           wage = Double.parseDouble(wageString);
           hoursString = JOptionPane.showInputDialog(null, "How many hours per week do you work?", "Hours Worked", JOptionPane.QUESTION_MESSAGE);
           hours = Integer.parseInt(hoursString);
           employees[count] = new HourlyEmployee(firstName, lastName, ssn, wage, hours);
           count++;
           break;

        case 3:
           salesString = JOptionPane.showInputDialog(null, "What are your weekly gross sales?", "Gross Sales", JOptionPane.QUESTION_MESSAGE);
           sales = Double.parseDouble(salesString);
           rateString = JOptionPane.showInputDialog(null, "What percent of commission do you receive?", "Commission Rate", JOptionPane.QUESTION_MESSAGE);
           rate = Double.parseDouble(rateString);
           employees[count] = new CommissionEmployee(firstName, lastName, ssn, sales, rate);
           count++;
           break;

        case 4:
           salesString = JOptionPane.showInputDialog(null, "What are your weekly gross sales?", "Gross Sales", JOptionPane.QUESTION_MESSAGE);
           sales = Double.parseDouble(salesString);
           rateString = JOptionPane.showInputDialog(null, "What percent of commission do you receive?", "Commission Rate", JOptionPane.QUESTION_MESSAGE);
           rate = Double.parseDouble(rateString);
           salaryString = JOptionPane.showInputDialog(null, "What is your base pay?", "Base Pay", JOptionPane.QUESTION_MESSAGE);
           salary = Double.parseDouble(salaryString);
           employees[count] = new BasePlusCommissionEmployee(firstName, lastName, ssn, sales, rate, salary);
           count++;
           break;

        default:
           JOptionPane.showMessageDialog(null, "Invalid entry!", "Error", JOptionPane.ERROR_MESSAGE);
           break;
     }
  }

额外优化说明

  • 通用信息采集逻辑抽离后,后续需要修改姓名、SSN的采集规则只需要改一处,维护成本更低
  • 调整count自增逻辑后,用户输入非法员工类型时不会浪费数组位置,保证可以录满5个有效员工

内容的提问来源于stack exchange,提问作者Jmac

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 05:42:02