我正在尝试创建一个由三个String组成的数组,该数组元素的值是我的姓名,性别和电子邮件。然后编写一个for循环以打印出每个元素。
我收到一条错误消息:当前项目中存在错误。
帮助我的代码中的错误是非常好的。
public class arraytest {
private String firstName;
private String gender;
private String emailA;
/** Constructor */
public arraytest(String firstName, String gender, String emailA){
this.firstName = firstName;
this.gender = gender;
this.emailA = emailA;
}
/** Getter - Returns the first name */
public String getFirstName() {
return firstName;
}
/** Return the gender */
public String getGender() {
return gender;
}
/** Returns the email address */
public String getEmailA() {
return emailA;
}
public void setInfo(String firstName, String gender, String emailA){
this.firstName = firstName;
this.gender = gender;
this.emailA = emailA;
}
public static void main (String[] args) {
String[] myInfo = new String[3];
arraytest test = new arraytest("Cameron","Male","cam@live.com");
myInfo[0] = test.firstName;
myInfo[1] = test.gender;
myInfo[2] = test.emailA;
for (int i=0; i<myInfo.length; i++) {
System.out.println(myInfo[i]);
}
}
}
答案 0 :(得分:0)
您创建了一个已经包含这3个元素的类。如果要将它们放在数组中,可以执行以下操作:
public static void main (String[] args) {
arraytest[] person = new arraytest[3];
test = new arraytest("Cameron","Male","cam@live.com");
person[0] = test.firstName;
person[1] = test.gender;
person[2] = test.firstName;
for (int i=0; i<person.length; i++) {
System.out.println(person[i]);
}
}