【问题标题】:Searching through a list of child subjects搜索子主题列表
【发布时间】:2020-03-31 01:48:14
【问题描述】:

所以我的问题是尝试在 main 方法中使用 getName() 和 getPhone() 方法(在 Contact 类中)。

我不知道如何在这种情况下将它们与所有 3 个类一起使用。

Here is what the program is supposed to be doing

抱歉,如果这不是太简洁,我最近才开始编程。

public class LookupPhonebook{
    public static void main(String[] args) {
        Phonebook phonebook = new Phonebook("Sam Johnson");
        phonebook.addContact(new Contact("Kelly Wong", "(02) 12345678"));
        phonebook.addContact(new Contact("Richard Jackson", "(02) 87654321"));
        phonebook.show();

        String searchName = Input.askString("Enter a contact name: ");
        phonebook.findContactByName(searchName);
        if (searchName.equals(phonebook.getName())) {
            System.out.println("Phone number is " + phonebook.getPhone());
        }
        else {
            System.out.println(searchName + " not found");
        }
    }
}

import java.util.*;
public class Phonebook {
    private String owner;
    private ArrayList<Contact> contacts = new ArrayList<Contact>();

    public Phonebook(String owner) {
        this.owner = owner;
    }

    public void addContact(Contact contact) {
        contacts.add(contact);
    }

    public void show(){
        System.out.println(owner + "'s phonebook");
        for (Contact contact : contacts) {
            System.out.println(contact);
        }
    }

    public Contact findContactByName(String name) {
        for (Contact contact : contacts) {
            if (contact.getName().equals(name)) {
                return contact;
            }
            else {
                continue;
            }
        }
        return null;    
    }
}

public class Contact {
    private String name;
    private String phone;

    public Contact(String name, String phone) {
        this.name = name;
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String toString() {
        return name + ": " + phone;
    }
}

此方法是在提供的用于帮助练习的类中唯一使用的方法。

/**
 * Asks the user the given question, waits for the user to enter a
 * single character at the keyboard, and then returns this character.
 *
 * @param question the question to be printed
 * @return the character that the user entered as an answer to the question
 */
public static char askChar(String question) {
    System.out.print(question + " ");
    return scanner.nextLine().charAt(0);
}

【问题讨论】:

    标签: java parent-child bluej


    【解决方案1】:

    您需要按照指令将findContactByName 的结果存储到局部变量中。原来的程序只是把结果扔掉了。

    public static void main(String[] args) {
        Phonebook phonebook = new Phonebook("Sam Johnson");
        phonebook.addContact(new Contact("Kelly Wong", "(02) 12345678"));
        phonebook.addContact(new Contact("Richard Jackson", "(02) 87654321"));
        phonebook.show();
    
        String searchName = Input.askString("Enter a contact name: ");
        // 2. Search contact and store is local variable
        Contact result = phonebook.findContactByName(searchName);
        // 3. If we found contact
        if (result != null) {
            System.out.println("Phone number is " + result.getPhone());
        } else {
        // 4. No contact is found
            System.out.println(searchName + " not found");
        }
    }
    

    【讨论】:

    • 非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    相关资源
    最近更新 更多