我该怎么做才能让用户选择一个人的性别?
How do I make it so that the user can choose the gender of a person?
请告诉我,我在枚举构造函数中放置了一个变量,用于显示男性和女性的性别。我希望用户选择他们的性别。例如,1-丈夫,2-妻子。我该怎么做?
Gender gen = Gender.MAN;
Person person1 = new Person(gen);
ArrayList <Person> people = new ArrayList<>();
people.add(person1);
}
}```
``` public class Person {
private Gender gen;
public Person ( Gender gen){
this.gen = gen;
}```
``` public enum Gender {
MAN("Man"),
WOMAN("Woman");
private String translation;
Gender(String translation){
this.translation = translation;
}
public String getGender(){
return translation;
}
} ```
Artemy,你是这个意思吗?
public enum Gender {
MAN("Man", "1", "M"),
WOMAN("Woman", "2", "W");
private List<String> translation;
Gender(String... translation) {
this.translation = Arrays.asList(translation);
}
public static Gender getGender(String value) {
if (MAN.translation.contains(value)) {
return MAN;
} else if (WOMAN.translation.contains(value)) {
return WOMAN;
}
throw new IllegalArgumentException(value);
}
}
请告诉我,我在枚举构造函数中放置了一个变量,用于显示男性和女性的性别。我希望用户选择他们的性别。例如,1-丈夫,2-妻子。我该怎么做?
Gender gen = Gender.MAN;
Person person1 = new Person(gen);
ArrayList <Person> people = new ArrayList<>();
people.add(person1);
}
}```
``` public class Person {
private Gender gen;
public Person ( Gender gen){
this.gen = gen;
}```
``` public enum Gender {
MAN("Man"),
WOMAN("Woman");
private String translation;
Gender(String translation){
this.translation = translation;
}
public String getGender(){
return translation;
}
} ```
Artemy,你是这个意思吗?
public enum Gender {
MAN("Man", "1", "M"),
WOMAN("Woman", "2", "W");
private List<String> translation;
Gender(String... translation) {
this.translation = Arrays.asList(translation);
}
public static Gender getGender(String value) {
if (MAN.translation.contains(value)) {
return MAN;
} else if (WOMAN.translation.contains(value)) {
return WOMAN;
}
throw new IllegalArgumentException(value);
}
}