Java为什么会出现这样的错误?
import java.util.*;
public class enrollment {
static String sName;
static int sAge;
static int sID;
static String sAddress;
static String sGender;
enrollment(String sName, int sAge, int sID, String sAddress, String sGender){
this.sName=sName;
this.sAge=sAge;
this.sID=sID;
this.sAddress=sAddress;
this.sGender=sGender;
}
public String getSName() {
return sName;
}
public int getSAge() {
return sAge;
}
public int getSID() {
return sID;
}
public String getSAddress() {
return sAddress;
}
public String getGender() {
return sGender;
}
public static void main(String[] args) {
LinkedList<enrollment> studentData = new LinkedList<enrollment>();
Scanner input = new Scanner(System.in);
System.out.println("Enter student name");
sName = input.nextLine();
studentData.add(sName);
System.out.println("Enter student age");
sAge = input.nextInt();
studentData.add(sAge);
System.out.println("Enter student ID");
sID = input.nextInt();
studentData.add(sID);
System.out.println("Enter student address");
sAddress = input.nextLine();
studentData.add(sAddress);
System.out.println("Enter student gender");
sGender = input.nextLine();
studentData.add(sGender);
System.out.println(studentData);
}
}
我正在尝试为一个学校项目制作一个注册程序,该程序需要一个人的姓名、年龄、身份证号码、地址和性别,然后在用户输入所有要求的信息后打印出来。我不明白我做错了什么。我觉得我做的每件事都是对的。我不确定我能做些什么来修复这些错误。导致这些错误的原因是什么?如何修复它们
给出的错误是
The method add(enrollment) in the type LinkedList<enrollment> is not applicable for the arguments (String)
The method add(int, enrollment) in the type LinkedList<enrollment> is not applicable for the arguments (int)
The method add(int, enrollment) in the type LinkedList<enrollment> is not applicable for the arguments (int)
The method add(enrollment) in the type LinkedList<enrollment> is not applicable for the arguments (String)
The method add(enrollment) in the type LinkedList<enrollment> is not applicable for the arguments (String)
at enrollment.main(enrollment.java:44)
# 1 楼答案
首先需要创建注册对象,如
然后设置“添加到列表”
或者创建setter
最后
# 2 楼答案
编译器告诉您不能将字符串和整数放入
enrollment
列表中。您可能需要创建一个enrollment
实例并将其添加到列表中:顺便说一句,您违反了Java标准命名约定:也就是说,类名应该以大写字母开头,所以最好使用
Enrollment
而不是enrollment
作为类名