0

この方法に問題があります。ユーザーが要求したときに正しい検索を出力しません。

これが私のコードです:

System.out.println("Search by Email.");
Employee employeeSearchEmail = MenuMethods.userInputByEmail();
Store.searchByEmail(employeeSearchEmail.getEmployeeEmail());

public Employee searchByEmail(String employeeEmail) {
    for (Employee employee : map.values()) {
        System.out.println(employee);
        map.equals(getClass());
        map.equals(employee.getEmployeeEmail());
        employee = new Employee(employeeEmail);
        ;
        return employee;
    }
    return null;
}

public static Employee userInputByEmail() {
    // String temp is for some reason needed. If it is not included
    // The code will not execute properly.
    String temp = keyboard.nextLine();
    Employee e = null;
    System.out.println("Please enter the Employee Email:");
    String employeeEmail = keyboard.nextLine();
    // This can use the employeeName's constructor because java accepts the
    // parameters instead
    // of the name's.
    return e = new Employee(employeeEmail);

}
4

3 に答える 3

1

問題は、このようなプログラムにif条件がないことです。

public Employee searchByEmail(String employeeEmail) {
        for (Employee employee : map.values()) {
            map.equals(getClass());
            if (map.equals(employee.getEmployeeEmail())){
                System.out.println(employee);
                return employee;
            }
        }
        return null;
    }

この行:System.out.println(employee);

一致するものが見つかるまで従業員オブジェクトを出力し、一致するとその従業員オブジェクトを返します。

于 2012-07-21T13:21:13.090 に答える
0

あなたはこのように言うべきです

if(employeeEmail.equals(employee.getEmployeeEmail()) return employee; 

Employeeオブジェクトの新しいインスタンスを作成する必要はありません。

于 2012-07-21T12:53:57.270 に答える
0

特定の電子メールアドレスを持つ従業員を返したい。したがって、現在の従業員の電子メールアドレスが指定された電子メールアドレスと等しい場合にのみ戻る必要があります。

 public Employee searchByEmail(String employeeEmail) 
    {
            for(Employee employee : map.values())
            {
                if(employee.getEmployeeEmail().equalsIgnoreCase(employeeEmail.trim()))
                          return employee;
            }
            return null;
    }

ちなみに、地図の鍵は何ですか。メールアドレスがキーである場合は、次のように簡単に返すことができます。

   return map.get(employeeEmail);
于 2012-07-21T12:55:09.753 に答える