错误消息是:
StudentArray.java:14: error: constructor Student in class Student cannot be applied to given types;
roster.add(new Student(name, id));
^
required: no arguments
found: String,String
reason: actual and formal argument lists differ in length
它重复了几次,并为numberOfStudents变量提供了Can not Find Symbol错误。这是程序,它尚未完成,但如果不解决这个问题我就无法继续前进。谢谢你的帮助!
import java.util.ArrayList;
import java.util.Scanner;
public class StudentArray {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Student> roster = new ArrayList<>();
int numberOfStudents = 0;
}
/** Add student method, includes student name and ID */
public void addStudent(String name, String id) {
roster.add(new Student(name, id));
numberOfStudents++;
}
/** Overloaded addStudent method to include grades */
public void addStudent(String name, String id, int grade1, int grade2, int grade3) {
roster.add(new Student(name, id, grade1, grade2, grade3));
numberOfStudents++;
}
/** Method to enter test grades */
public void enterTestGrades(int testNumber) {
int data = -1;
for (int index : roster) {
System.out.println("Enter the grade for " + roster.get(index) + ":");
data = input.nextInt();
roster.set(index, data);
}
}
public void displayStudentInformation() {
}
public void displayMenu() {
}
}
/** Student Class */
public class Student2 {
private String studentName;
private String studentId;
private final int NUMBER_OF_GRADES = 3;
private int[] studentGrades = new int[NUMBER_OF_GRADES];
private int averageGrade;
private final int DEFAULT_GRADE = -1;
/** No arg constructor */
public Student2() {
for (int index = 0; index < NUMBER_OF_GRADES; index++) {
this.studentGrades[index] = DEFAULT_GRADE;
}
}
/** Constructor that sets Student name and ID */
public Student2(String name, String id) {
this();
this.studentName = name;
this.studentId = id;
}
/** Constructor that sets all Student data */
public Student2(String name, String id, int grade1, int grade2, int grade3) {
this(name, id);
this.studentGrades[0] = grade1;
this.studentGrades[1] = grade2;
this.studentGrades[2] = grade3;
}
/** Accessor for studentName */
public String getStudentName() {
return this.studentName;
}
/** Accessor for studentId */
public String getStudentID() {
return this.studentId;
}
/** Accessor for student grades */
public int getStudentGrade(int index) {
return this.studentGrades[index];
}
/** Return average student grade */
public int getAverageStudentGrade() {
int temp = 0;
for (int index = 0; index < NUMBER_OF_GRADES; index++) {
temp += getStudentGrade(index);
}
int averageStudentGrade = temp / NUMBER_OF_GRADES;
return averageStudentGrade;
}
/** Return letter grade */
public char getLetterGrade(int grade) {
int temp = grade / 10;
char letterGrade;
switch (temp) {
case 10:
case 9: letterGrade = 'A';
break;
case 8: letterGrade = 'B';
break;
case 7: letterGrade = 'C';
break;
case 6: letterGrade = 'D';
break;
default: letterGrade = 'F';
break;
}
return letterGrade;
}
/** Setter method for studentName */
public void setStudentName(String name) {
this.studentName = name;
}
/** Setter method for studentId */
public void setStudentId(String id) {
this.studentId = id;
}
/** Setter for studentGrade */
public void setStudentGrade(int grade, int index) {
this.studentGrades[index] = grade;
}
}
非常感谢你能帮助我。我完全陷入困境,这是我的第一个java课程的最终项目。
答案 0 :(得分:1)
错误消息实际上非常明确:它无法创建Student实例,因为它找不到任何以两个字符串作为参数的构造函数。
这意味着您的班级Student
(不是Student2
)没有此构造函数。