gpt4 book ai didi

java - 搜索子主题列表

转载 作者:行者123 更新时间:2023-12-01 17:41:44 25 4
gpt4 key购买 nike

所以我的问题是尝试在主方法中使用 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;
}
}

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

/**
* 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);
}

最佳答案

您需要按照指令将 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");
}
}

关于java - 搜索子主题列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60942210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com