0
這是我見過的一個基本的Java實踐問題,但我希望將它提升一個檔次,幷包含一些功能,這些功能會詢問用戶是否要修改員工的信息,如果是這樣,那麼應用所請求的修改員工並再次顯示更新的員工信息。如何詢問用戶是否想修改信息並應用更改?
這裏是我的代碼至今:
public class Employee
{
private String firstName;
private String lastName;
private double monthlySalary;
private String decision;
// constructor to initialize first name, last name and monthly salary
public Employee(String first, String last, double salary)
{
firstName = first;
lastName = last;
if (salary >= 0.0) // determine whether salary is positive
monthlySalary = salary;
} // end three-argument Employee constructor
// set Employee's first name
public void setFirstName(String first)
{
firstName = first;
} // end method setFirstName
// get Employee's first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// set Employee's last name
public void setLastName(String last)
{
lastName = last;
} // end method setLastName
// get Employee's last name
public String getLastName()
{
return lastName;
} // end method getLastName
// set Employee's monthly salary
public void setMonthlySalary(double salary)
{
if (salary >= 0.0) // determine whether salary is positive
monthlySalary = salary;
} // end method setMonthlySalary
// get Employee's monthly salary
public double getMonthlySalary()
{
return monthlySalary;
} // end method getMonthlySalary
// set Employee's new monthly salary
public void setNewMonthlySalary(double salary)
{
monthlySalary = salary;
} // end method setMonthlySalary
// get Employee's new monthly salary
public double getNewMonthlySalary()
{
return monthlySalary;
} // end method getMonthlySalary
} // end class Employee
和EmployeeTest類
import java.util.Scanner;
public class EmployeeTest
{
public static void main(String args[])
{
Employee employee1 = new Employee("Bo", "Jackson", 8875.00);
Employee employee2 = new Employee("Cam", "Newton", 13150.75);
// create Scanner to obtain input from command window
Scanner input = new Scanner(System.in);
// display employees
System.out.printf("Employee 1: %s %s; Yearly Salary: %.2f\n",
employee1.getFirstName(), employee1.getLastName(),
12 * employee1.getMonthlySalary());
System.out.printf("Employee 2: %s %s; Yearly Salary: %.2f\n",
employee2.getFirstName(), employee2.getLastName(),
12 * employee2.getMonthlySalary());
// enter new employee salaries
System.out.println("Enter " + employee1.getFirstName() + "'s new salary:");
double newSalary1 = input.nextDouble();
employee1.setNewMonthlySalary(newSalary1);
System.out.println("Enter " + employee2.getFirstName() + "'s new salary:");
double newSalary2 = input.nextDouble();
employee2.setNewMonthlySalary(newSalary2);
// display employees with new yearly salary
System.out.printf("Employee 1: %s %s; Yearly Salary: %.2f\n",
employee1.getFirstName(), employee1.getLastName(),
12 * newSalary1);
System.out.printf("Employee 2: %s %s; Yearly Salary: %.2f\n",
employee2.getFirstName(), employee2.getLastName(),
12 * newSalary2);
} // end main
} // end class EmployeeTest
顯示僱員的信息後,我想提示用戶選擇是否要做出改變到員工的工資。如果是這樣,那麼哪個員工。更改完成後,我想顯示修改後的結果。處理這個問題最簡單的方法是什麼?
考慮使用諸如「displayEmployee」,「newEmployee」,「updateEmployee」之類的方法,這可能會幫助您找出答案。 –
我真的想知道如果我應該使用if語句或switch語句嗎?真的不需要命名方法的幫助。如果我能得到一個if語句的例子,它將完成四個步驟:1)提示用戶輸入yes或no; 2)如果是,則提示用戶選擇員工一或員工二; 3)然後,一旦僱員被選中,他們可以修改工資; 4)然後,顯示新的工資。 – NOSDUH
在我看來,你想知道如果有人可以做你的功課。你應該「嘗試一些東西」(假設使用'if',因爲*規則* 2選擇=>'if',更多選擇=>'switch'),然後詢問是否遇到任何特定問題。祝你好運。 –