【问题标题】:How can I ask a user if they want to modify information and then apply the change?我如何询问用户是否要修改信息然后应用更改?
【发布时间】:2015-09-14 15:56:07
【问题描述】:

这是我见过的一个基本 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)提示用户输入是或否; 2)然后,如果是,则提示用户选择员工一或员工二; 3)然后,一旦选择了员工,他们就可以修改工资; 4) 然后,显示新的工资。
  • 在我看来,您想知道是否有人可以做您的作业。您应该“尝试一些事情”(假设使用 if,因为 经验法则 2 个选择 => if,更多选择 => switch)然后询问您是否遇到任何具体问题.祝你好运。

标签: java eclipse if-statement switch-statement printf


【解决方案1】:

在对我的嵌套 ifs 语句进行一些调整后,我想出了一种方法来实现我所寻找的。我不能说我正在寻找家庭作业的答案,因为我不再在学校了。我只是在寻找一种更简单的方法来实现我的目标,走阻力最小的道路......这会使编码更容易和更简洁。尽管我的答案比我想要的要大一些,但它仍然可以完成手头的任务。我将更加努力地改进应用程序,但这是我想出的:

import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.JDialog;

public class EmployeeTest
{

    public static void main( String args[] )
    {
        // create Scanner to obtain input from command window
        Scanner input = new Scanner(System.in);
        Employee employee1 = new Employee( "Bo", "Jackson", 8875.00 );
        Employee employee2 = new Employee( "Cam", "Newton", 13150.75 );

        // 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() );

        JDialog.setDefaultLookAndFeelDecorated(true);
        int response1 = JOptionPane.showConfirmDialog(null, "Do you wnat to change an employee's salary?", 
                "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

            if (response1 == JOptionPane.NO_OPTION)// if NO is clicked
            {
                System.out.println("See you next time");
            } else if (response1 == JOptionPane.YES_OPTION)// if YES is clicked
            {
                // option to change the salary for employee 1
                int response2 = JOptionPane.showConfirmDialog(null, "Would you like to change " + employee1.getFirstName() + " " + employee1.getLastName() + "'s salary:", 
                        "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if (response2 == JOptionPane.NO_OPTION)// if NO is clicked
                {
                    // option to change the salary for employee 2
                    int response3 = JOptionPane.showConfirmDialog(null, "Would you like to change " + employee2.getFirstName() + " " + employee2.getLastName() + "'s salary:", 
                            "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                    if (response3 == JOptionPane.NO_OPTION)
                            {
                                System.out.println("See you next time");
                            } else if (response3 == JOptionPane.YES_OPTION)// if YES is clicked
                            {
                                // enter new salary for employee 2
                                System.out.println( "Enter " + employee2.getFirstName() + " " + employee2.getLastName() + "'s new salary:" );
                                double newSalary2 = input.nextDouble();
                                employee2.setNewMonthlySalary( newSalary2 );

                                // display unchanged salary for employee 1
                                System.out.printf( "Employee 1: %s %s; Yearly Salary: %.2f\n",
                                        employee1.getFirstName(), employee1.getLastName(),
                                        12 * employee1.getMonthlySalary() );

                                // display new yearly salary for employee 2
                                System.out.printf( "Employee 2: %s %s; Yearly Salary: %.2f\n",
                                        employee2.getFirstName(), employee2.getLastName(),
                                        12 * newSalary2 );
                            } else if (response3 == JOptionPane.CLOSED_OPTION)// if JOPTIONPANE is closed
                            {
                                System.out.println("JOptionPane closed");
                            }// END RESPONSE 3
                } else if (response2 == JOptionPane.YES_OPTION)// if YES is clicked
                {
                    // enter new salary for employee 1
                    System.out.println( "Enter " + employee1.getFirstName() + " " + employee1.getLastName() + "'s new salary:" );
                    double newSalary1 = input.nextDouble();
                    employee1.setNewMonthlySalary( newSalary1 );

                    // display new yearly salary for employee 1
                    System.out.printf( "Employee 1: %s %s; Yearly Salary: %.2f\n",
                            employee1.getFirstName(), employee1.getLastName(),
                            12 * newSalary1 );
                    // display unchanged salary for employee 2
                    System.out.printf( "Employee 2: %s %s; Yearly Salary: %.2f\n",
                            employee2.getFirstName(), employee2.getLastName(),
                            12 * employee2.getMonthlySalary() );
                } else if (response2 == JOptionPane.CLOSED_OPTION)// if JOPTIONPANE is closed
                {
                    System.out.println("JOptionPane closed");
                }// END RESPONSE 2
                {

                }

            } else if (response1 == JOptionPane.CLOSED_OPTION)// if JOPTIONPANE is clicked
            {
                System.out.println("JOptionPane closed");
            }// END RESPONSE 1

    } // end main

 } // end class EmployeeTest

上述帖子的公共类“员工”没有改变。

【讨论】:

    猜你喜欢
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    相关资源
    最近更新 更多