Java Help Question Nested If Statement?

The “Employee Bonus” output is where I’m having problems. It’s coming out as .03 which is the percent of the bonus, but it’s not multiplying BONUS_3 * employeeSalary. Is something declared incorrectly?

The output should read like this:

Employee Name: Laurie Blair

Employee Salary: 650.0

Employee Rating: 3

Employee Bonus: $1950.0 //But it’s outputting .03

For the assignment, only code after “//Write your code here.” should be changed

// EmployeeBonus.java – This program calculates an employee’s yearly bonus.

import javax.swing.*;

public class EmployeeBonus

{

public static void main(String args[])

{

// Declare and initialize variables here.

String employeeName;

String salaryString;

double employeeSalary;

String ratingString;

int employeeRating;

double employeeBonus;

final double BONUS_1 = .10;

final double BONUS_2 = .06;

final double BONUS_3 = .03;

final double NO_BONUS = 0.;

final int RATING_1 = 1;

final int RATING_2 = 2;

final int RATING_3 = 3;

employeeName = JOptionPane.showInputDialog(“Enter employee’s name: “);

salaryString = JOptionPane.showInputDialog(“Enter employee’s yearly salary:”);

ratingString = JOptionPane.showInputDialog(“Enter employee’s performance rating: “);

employeeSalary = Double.parseDouble(salaryString);

employeeRating = Integer.parseInt(ratingString);

// Write your code here.

if(employeeRating == RATING_1)

employeeBonus = BONUS_1 * employeeSalary;

else if (employeeRating == RATING_2)

employeeBonus = BONUS_2 * employeeSalary;

else if (employeeRating == RATING_3)

employeeBonus = BONUS_3 * employeeSalary;

else

employeeBonus = NO_BONUS;

// Output.

System.out.println(“Employee Name ” + employeeName);

System.out.println(“Employee Salary $” + employeeSalary);

System.out.println(“Employee Rating ” + employeeRating);

System.out.println(“Employee Bonus $” + employeeBonus);

System.exit(0);

}

}

Update:

It sure is.. Thank you Arpita! /embarassed

2

✅ Answers

? Favorite Answer

  • I tested tht code.. its working perfectly.

  • I pasted your code, and did auto-format. That added the brackets and program runs fine…

    if (employeeRating == RATING_1) {

    employeeBonus = BONUS_1 * employeeSalary;

    } else if (employeeRating == RATING_2) {

    employeeBonus = BONUS_2 * employeeSalary;

    } else if (employeeRating == RATING_3) {

    employeeBonus = BONUS_3 * employeeSalary;

    } else {

    employeeBonus = NO_BONUS;

    }

    take out the System.exit(0); not needed at all

  • Leave a Comment