如何确定是否选择了开关盒?
How to determine whether a switch case was chosen?
我有一个正在处理的项目,它要求我有一个数组,其大小设置为 5。然后我需要从 4 个不同的案例中收集数据,并将它们存储 5 次。 (其中一个案例可以多次使用,案例由用户输入指定)。这是我尝试过的:
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);
switch (employeeType) {
case 1:
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);
salaryString = JOptionPane.showInputDialog(null, "What is your weekly salary?", "Salary", JOptionPane.QUESTION_MESSAGE);
salary = Double.parseDouble(salaryString);
break;
case 2:
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);
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);
break;
case 3:
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);
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?", "Commsission Rate", JOptionPane.QUESTION_MESSAGE);
rate = Double.parseDouble(rateString);
break;
case 4:
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);
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?", "Commsission 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);
break;
default:
JOptionPane.showMessageDialog(null, "Invalid entry!", "Error", JOptionPane.ERROR_MESSAGE);
break;
}
switch (count) {
case 0:
employees[0] = new SalariedEmployee(firstName, lastName, ssn, salary);
break;
case 1:
employees[1] = new HourlyEmployee(firstName, lastName, ssn, wage, hours);
break;
case 2:
employees[2] = new CommissionEmployee(firstName, lastName, ssn, sales, rate);
break;
case 3:
employees[3] = new BasePlusCommissionEmployee(firstName, lastName, ssn, sales, rate, salary);
break;
case 4:
employees[4] = new SalariedEmployee(firstName, lastName, ssn, salary);
}
}
我计划用其他东西替换最后一个 Switch(count),但我不确定如何去做。我遇到的问题是,无论我输入什么, Switch(count) 总是会迭代到下一个并且不关心我选择了什么。因此,如果我选择员工类型 1,然后选择员工类型 3,它会询问我类型 3 的信息,但会显示类型 2 的所有信息(导致空白结果)。
salaried employee: john smith
social security number: 111-11-1111
date of birth: null
weekly salary: 0.00
earned ,200.00
hourly employee: sue jones
social security number: 222-22-2222
date of birth: null
hourly wage: [=11=].00; hours worked: 0.00
earned [=11=].00
commission employee: karen price
social security number: 333-33-3333
date of birth: null
gross sales: ,000.00; commission rate: 0.06
earned ,400.00
base-salaried commission employee: bob lewis
social security number: 444-44-4444
date of birth: null
gross sales: ,000.00; commission rate: 0.04; base salary: 0.00
new base salary with 10% increase is: 0.00
earned ,120.00
salaried employee: jomes allen
date of birth: null
weekly salary: 0.00
earned ,200.00
Employee 0 is a SalariedEmployee
Employee 1 is a HourlyEmployee
Employee 2 is a CommissionEmployee
Employee 3 is a BasePlusCommissionEmployee
Employee 4 is a SalariedEmployee
如上图,输出显示了一个Salaried, Hourly, Commission, BasePlus, then Salaried employee,当我实际输入了一个Salaried, then Salaried。
可能值得将 EmployeeType 封装为单独的实体 class/enum 将值返回给 create/returning 创建的员工对象。
如果我理解正确,这应该可以工作:
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);
switch (employeeType) {
case 1:
// input data (removed for brevity)
employees[count] = new SalariedEmployee(firstName, lastName, ssn, salary);
break;
case 2:
// input data (removed for brevity)
employees[count] = new HourlyEmployee(firstName, lastName, ssn, wage, hours);
break;
case 3:
// input data (removed for brevity)
employees[count] = new CommissionEmployee(firstName, lastName, ssn, sales, rate);
hours);
break;
case 4:
// input data (removed for brevity)
employees[count] = new BasePlusCommissionEmployee(firstName, lastName, ssn, sales, rate, salary);
break;
default:
JOptionPane.showMessageDialog(null, "Invalid entry!", "Error", JOptionPane.ERROR_MESSAGE);
count--; // Subtract 1 so the loop repeats
break;
}
}
我有一个正在处理的项目,它要求我有一个数组,其大小设置为 5。然后我需要从 4 个不同的案例中收集数据,并将它们存储 5 次。 (其中一个案例可以多次使用,案例由用户输入指定)。这是我尝试过的:
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);
switch (employeeType) {
case 1:
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);
salaryString = JOptionPane.showInputDialog(null, "What is your weekly salary?", "Salary", JOptionPane.QUESTION_MESSAGE);
salary = Double.parseDouble(salaryString);
break;
case 2:
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);
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);
break;
case 3:
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);
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?", "Commsission Rate", JOptionPane.QUESTION_MESSAGE);
rate = Double.parseDouble(rateString);
break;
case 4:
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);
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?", "Commsission 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);
break;
default:
JOptionPane.showMessageDialog(null, "Invalid entry!", "Error", JOptionPane.ERROR_MESSAGE);
break;
}
switch (count) {
case 0:
employees[0] = new SalariedEmployee(firstName, lastName, ssn, salary);
break;
case 1:
employees[1] = new HourlyEmployee(firstName, lastName, ssn, wage, hours);
break;
case 2:
employees[2] = new CommissionEmployee(firstName, lastName, ssn, sales, rate);
break;
case 3:
employees[3] = new BasePlusCommissionEmployee(firstName, lastName, ssn, sales, rate, salary);
break;
case 4:
employees[4] = new SalariedEmployee(firstName, lastName, ssn, salary);
}
}
我计划用其他东西替换最后一个 Switch(count),但我不确定如何去做。我遇到的问题是,无论我输入什么, Switch(count) 总是会迭代到下一个并且不关心我选择了什么。因此,如果我选择员工类型 1,然后选择员工类型 3,它会询问我类型 3 的信息,但会显示类型 2 的所有信息(导致空白结果)。
salaried employee: john smith
social security number: 111-11-1111
date of birth: null
weekly salary: 0.00
earned ,200.00
hourly employee: sue jones
social security number: 222-22-2222
date of birth: null
hourly wage: [=11=].00; hours worked: 0.00
earned [=11=].00
commission employee: karen price
social security number: 333-33-3333
date of birth: null
gross sales: ,000.00; commission rate: 0.06
earned ,400.00
base-salaried commission employee: bob lewis
social security number: 444-44-4444
date of birth: null
gross sales: ,000.00; commission rate: 0.04; base salary: 0.00
new base salary with 10% increase is: 0.00
earned ,120.00
salaried employee: jomes allen
date of birth: null
weekly salary: 0.00
earned ,200.00
Employee 0 is a SalariedEmployee
Employee 1 is a HourlyEmployee
Employee 2 is a CommissionEmployee
Employee 3 is a BasePlusCommissionEmployee
Employee 4 is a SalariedEmployee
如上图,输出显示了一个Salaried, Hourly, Commission, BasePlus, then Salaried employee,当我实际输入了一个Salaried, then Salaried。
可能值得将 EmployeeType 封装为单独的实体 class/enum 将值返回给 create/returning 创建的员工对象。
如果我理解正确,这应该可以工作:
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);
switch (employeeType) {
case 1:
// input data (removed for brevity)
employees[count] = new SalariedEmployee(firstName, lastName, ssn, salary);
break;
case 2:
// input data (removed for brevity)
employees[count] = new HourlyEmployee(firstName, lastName, ssn, wage, hours);
break;
case 3:
// input data (removed for brevity)
employees[count] = new CommissionEmployee(firstName, lastName, ssn, sales, rate);
hours);
break;
case 4:
// input data (removed for brevity)
employees[count] = new BasePlusCommissionEmployee(firstName, lastName, ssn, sales, rate, salary);
break;
default:
JOptionPane.showMessageDialog(null, "Invalid entry!", "Error", JOptionPane.ERROR_MESSAGE);
count--; // Subtract 1 so the loop repeats
break;
}
}