有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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)

共 (2) 个答案

  1. # 1 楼答案

    首先需要创建注册对象,如

    Enrollment enrollment = new Enrollment(" all the contstructor values here");
    

    然后设置“添加到列表”

    studentData.add(enrollment);
    

    或者创建setter

    Enrollment enrollment = new Enrollment();
    enrollment.setSName(input.nextLine());
    

    最后

    studentData.add(enrollment);
    
  2. # 2 楼答案

    编译器告诉您不能将字符串和整数放入enrollment列表中。您可能需要创建一个enrollment实例并将其添加到列表中:

    System.out.println("Enter student name");
    sName = input.nextLine();
    
    System.out.println("Enter student age");
    sAge = input.nextInt();
    
    System.out.println("Enter student ID");
    sID = input.nextInt();
    
    System.out.println("Enter student address");
    sAddress = input.nextLine();
    
    System.out.println("Enter student gender");
    sGender = input.nextLine();
    
    enrollment en = new enrollment(sName, sAge, sID, sAddress, sGender);
    
    studentData.add(sGender);
    

    顺便说一句,您违反了Java标准命名约定:也就是说,类名应该以大写字母开头,所以最好使用Enrollment而不是enrollment作为类名