有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java不能通过使用Scanner获取数据从一个类中创建两个对象

我是Java的初学者。我正试图从一个类中创建两个Scanner对象(我自己创建了这个类,在那里我还创建了一个构造函数)。我已经通过构造函数调用初始化了这两个对象,稍后我想通过按Scanner类从user获取数据来更新这两个对象的信息。当我通过使用Scanner对象从用户获取数据来创建对象时,我只能创建一个对象,并且第二个对象的代码在没有从用户获取任何数据的情况下完全执行。因此,我只能创建一个对象,而第二个对象的代码只显示在执行窗口中,而不获取任何数据

当两个对象已经通过构造函数调用初始化时,我是否不能通过扫描仪更新它们的数据

下面是我想从中创建两个对象的类代码:

public class Employee {

//instance variables
String firstName;
String lastName;
double salary;

//constructor for three arguments
public Employee(String first_name, String last_name, double month_salary)
{
    firstName = first_name;
    lastName = last_name;
    if(month_salary > 0) //make sure that the salary is not smaller than zero

    {
        salary = month_salary;
    }
}//end of constructor

//set the value of the instance variables
        //set first name        
        public void setFirstName(String first_name)
        {
            firstName = first_name;
        }
        //get first name
        public String getFirstName()
        {
            return firstName;
        }

        //set last name
        public void setLastName(String last_name)
        {
            lastName = last_name;
        }

        //get last name
        public String getLastName()
        {
            return lastName;
        }

        //set salary
        public void setSalary(double month_salary)
        {
            if(month_salary > 0) //make sure that the salary is not smaller than zero

            {
                salary = month_salary;
            }
            else salary = 0.00;
        }

        //get salary
        public double getSalary()
        {
            return salary;
        }
   }

这是对象的代码

import java.util.Scanner;
public class EmployeeTest {

public static void main(String[] args) {

    Employee employee1 = new Employee("M", "R", 5000);
    Employee employee2 = new Employee("T", "M", 6000);

    System.out.println("Display the intial Employee's information\n");

    //showing the Employee2's information
    System.out.println("Displaying Employee1's information");

    System.out.printf("First Name: %s\nLast Name: %s\nSalary: %.2f\n", 
            employee1.getFirstName(), employee1.getLastName(), employee1.getSalary());

    System.out.println();

    //showing the Employee2's information
    System.out.println("Displaying Employee2's information");

    System.out.printf("First Name: %s\nLast Name: %s\nSalary: %.2f\n", 
            employee2.getFirstName(), employee2.getLastName(), employee2.getSalary());

    System.out.println();

    //create an scanner object to get information from user
    Scanner input = new Scanner(System.in);

    //obtain employee's first name from user
    System.out.print("Please enter the first name of employee1: ");
    String fName1 = input.nextLine();

    //set the first of employee1
    employee1.setFirstName(fName1);

    //obtain employee's last name from user
    System.out.print("Please enter the last name of employee1: ");
    String lName1 = input.nextLine();

    //set the last of employee1
    employee1.setLastName(lName1);

    //set the salary employee1's
    System.out.print("Please enter employee's salary: ");
    double empl_salary1 = input.nextDouble();

    //obtain the employe1's salary
    employee1.setSalary(empl_salary1);

    //display the updated employee1's information



      System.out.println("Updated information of employee's information");

    System.out.printf("First name :%s\nLast name: %s\nSalary: %.2f\n",
            employee1.getFirstName(),employee1.getLastName(), employee1.getSalary());

    //obtain employee2's first name from user   
    System.out.print("Please enter employee2'first name: ");
    String fName2 = input.nextLine();

    //set the first name of employee2
    employee2.setFirstName(fName2);


    //obtain employee's last name from user
    System.out.print("Please enter the employee2's last name: ");
    String lName2 = input.nextLine();

    //set the last name of employee2
    employee2.setLastName(lName2);

    //obtain the salary employee2's
    System.out.print("Please enter employee2's salary: ");
    double empl_salary2 = input.nextDouble();

    //set the employe1's salary
    employee2.setSalary(empl_salary2);


}

}

我不知道问题出在哪里

谢谢你的时间和帮助


共 (1) 个答案

  1. # 1 楼答案

    在获取第二名员工的数据之前添加input.nextLine();

    input.nextLine();
    
    //obtain employee2's first name from user   
    System.out.print("Please enter employee2'first name: ");
    String fName2 = input.nextLine();
    

    这是因为input.nextDouble();“消耗”的值只有两倍,而您输入的换行符则为“未消耗”