gpt4 book ai didi

java - 根据用户输入设置枚举

转载 作者:搜寻专家 更新时间:2023-11-01 03:04:10 25 4
gpt4 key购买 nike

<分区>

重新选择 Java 并尝试熟悉枚举类型。我正在尝试创建一个地址簿,用户可以在其中创建联系人。我已经创建了所有内容,但挂断了设置联系人类型(家庭、 friend 、企业等...) 我在单独的 java 类中设置了一个枚举类。

public class ContactType
{
public enum contactType
{
Family,
Church,
Friend,
BusninessColleague,
ServicePerson,
Customer,
Other
}
}

我的联系人类看起来像:

public class Contacts
{
private contactType contact;
private String name;
private String streetAddress;
private String city;
private String state;
private String zipCode;
private String phone;
private String email;
private String photo;


public Contacts ( )
{
contact = null;
name = "XXX XXX";
streetAddress = "XXX";
state = "XX";
zipCode = "00000";
phone = "XXX-XXXX";
email = "XXX@XXX.COM";
photo = "XXX.jpg";
}


public Contacts (ContactType contactType, String name, String streetAddress, String city,
String state, String zipCode, String phone, String email, String photo)
{

this.contact = contactType;
this.name = name;
this.streetAddress = streetAddress;
this.city = city;
this.state = state;
this.zipCode = zipCode;
this.phone = phone;
this.email = email;
this.photo = photo;
}



public ContactType getContactType ( )
{
return contact;
}


public void setContactType (ContactType input)
{
this.contact = input;
}

//rest of code

最后是我的驱动程序,它包含一个菜单(除了设置联系人类型之外其他一切都有效,所以为了保持简短我只包含了该片段):

switch (iSelection)
{
case 1:
c1 = new Contacts(); //creates a new contact
break;
case 2:
strContactType = JOptionPane.showInputDialog ("Please enter contact type (Family, Church, BusinessColleague, ServicePerson, Customer, or Other)");
contactType.valueOf(strContactType);
JOptionPane.showMessageDialog (null, strContactType);
c1.setContactType (strContactType);
break;

我知道我在这里做错了 c1.setContactType(strContactType); 因为我得到“联系人类型中的方法 setContactType(ContactType) 不适用于参数(字符串)"由于完全无知,我不知道如何修复它以将 contactType 设置为用户输入的任何内容。

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