为对象获取用户输入

Taking user input for the objects

问题:创建 class Employee 的两个对象并检查两者是否相同

下面的代码给出了一个错误:线程“main”中的异常java.util.InputMismatchException

只有对象 e1 接受值

 class Employee {
        String name;
        int age;
        char gender;

        public Employee() {
                super();
        }
        public Employee (String name, int age, char gender) {
            this.gender = gender;
            this.name = name;
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public char getGender() {
            return gender;
        }
        public void setGender(char gender) {
            this.gender = gender;
        }
}
public class Source {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            Employee e1 = new Employee();
    
            //e1.name = sc.nextLine();
            //e1.age = sc.nextInt();
            //e1.gender = sc.next().charAt(0);
            System.out.println(e1.name+" "+e1.age+" "+e1.gender);
            Employee e2 = new Employee();
            //e2.name = sc.nextLine();
            //e2.age = sc.nextInt();
            //e2.gender = sc.next().charAt(0);
            System.out.println(e2.name+" "+e2.age+" "+e2.gender);
    
            boolean isSame = e1.equals(e2);
            if(e1.equals(e2)) {
                System.out.println("Same");
            }
            else {
                System.out.println("Different");
            }
       }
}

对象 e1 和 e2 如何获取用户输入或键盘输入?

我已经对你的 Employee 和 Source 进行了一些修改 类,请看一下。

public class Source {

Scanner sc = null;

public static void main(String[] args) {
    
    Source source = new Source();
    
    Employee e1 = source.getEmployee();
    Employee e2 = source.getEmployee();

    if(e1.equals(e2)) {
        System.out.println("Same");
    }
    else {
        System.out.println("Different");
    }
    
    source.closeScanner();
}

public Source() {
    sc = new Scanner(System.in);
}

public void closeScanner() {
    sc.close();
}

public Employee getEmployee() {

    Employee e = new Employee();

    System.out.print("Enter Name: ");
    e.name = sc.next();

    System.out.print("Enter Age: ");
    e.age = sc.nextInt();


    System.out.print("Enter Gender: ");
    e.gender = sc.next().charAt(0);


    System.out.println(e.getName() + " " + e.getAge() + " " + e.getGender());

    return e;
}

}

public class Employee {

String name;
int age;
char gender;

public Employee() {
        super();
        int age = -1;
}
public Employee (String name, int age, char gender) {
    this.gender = gender;
    this.name = name;
    this.age = age;
}
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
public char getGender() {
    return gender;
}
public void setGender(char gender) {
    this.gender = gender;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + age;
    result = prime * result + gender;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Employee other = (Employee) obj;
    if (age != other.age)
        return false;
    if (gender != other.gender)
        return false;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
}

}

请注意,我添加了以下两种方法

public int hashCode()

public boolean equals(Object obj)

可以比较员工对象的两个实例