Java - 为什么我会遇到各种各样的错误?

时间:2017-12-01 19:56:23

标签: java

resolvers += "Flyway" at "https://flywaydb.org/repo"
addSbtPlugin("org.flywaydb" % "flyway-sbt" % "4.0")

我正在尝试为学校项目制作一个注册程序,该项目记录了人名,年龄,身份证号码,地址和性别,然后在用户输入所有要求的信息后将其全部打印出来。我不明白我做错了什么。我觉得我已经做好了一切。我不确定我能做些什么来解决这些错误。造成这些错误的原因是什么?如何解决?

给出的错误是

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);

    }
}

2 个答案:

答案 0 :(得分: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作为类名。

答案 1 :(得分:0)

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

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

然后将其添加到列表

studentData.add(enrollment);

或创建setter

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

然后终于

studentData.add(enrollment);