gpt4 book ai didi

java - ArrayList 错误 - 找不到符号

转载 作者:行者123 更新时间:2023-11-30 06:45:46 31 4
gpt4 key购买 nike

我正在为学校做一个期末项目,但遇到了一些麻烦。我从来没有用 Java 进行过任何编程,也没有进行过任何面向对象的编程,所以我的代码可能不会那么好。

我的数组列表遇到一些问题。我收到一条错误消息,指出找不到符号 - class StudentInfo。我的代码做错了什么?这阻止了我编译代码。所有学生信息都是假的。

还有人对如何清理或优化我的代码有任何建议吗?

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Roster
{
private static ArrayList<studentInfo> studentList = new ArrayList<>();

public static void main(String[] args)
{

add(1, "John", "Smith", "John1989@gmail.com", 20, 88, 79, 59);
add(2, "Suzan", "Erickson", "Erickson_1990@gmailcom", 19, 91, 72, 85);
add(3, "Jack", "Napoli", "The_lawyer99yahoo.com", 19, 85, 84, 87);
add(4, "Erin", "Black", "Erin.black@comcast.net", 22, 91, 98, 82);
print_all();
print_invalid_emails();
print_average_grade(2);
remove(3);
remove(3);



}
public static void add (int studentID, String firstName, String lastName, String email, int age, int grade1, int grade2, int grade3)
{
int[] grades = {grade1, grade2, grade3};
studentInfo newStudent = new studentInfo (studentID, firstName, lastName, email, age, grades);
studentList.add(newStudent);
}

public static void remove(int studentID)
{
for (studentInfo i: studentList) {
if (i.getStudentID == studentID) {
studentList.remove(i);
System.out.println("Student Removed");
}
else {
System.out.println("A student with this ID(studentID) was not found.");
return;
}

}
}
public static void print_all (){
for (studentInfo i: studentList) {
studentList.get(i).print();
}
}
public static void print_average_grade(int studentID) {
studentList.get(studentID).getAverageGrade();
}

public static void print_invalid_emails() {
String RFC5322 = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$";
Pattern pattern = Pattern.compile(RFC5322);
for (studentInfo i: studentList) {
String email = i.getEmail;
Matcher matcher = pattern.matcher(email);
System.out.println(email +":\t"+ matcher.matches());
}

}


}

我的第二堂课

public class Student
{
private int studentID;
private String firstName;
private String lastName;
private String email;
private int age;
private int[] grades;

public Student (int studentID, String firstName, String lastName, String email, int age, int grades[])
{
setStudentID(studentID);
setFirstName(firstName);
setLastName(lastName);
setEmail(email);
setAge(age);
setGrades(grades);

}

public void setStudentID(int studentID) {
this.studentID = studentID;
}

public void setFirstName(String firstName)
{
this.firstName = firstName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public void setEmail(String email) {
this.email = email;
}

public void setAge(int age) {
this.age = age;
}

public void setGrades(int[] grades) {
this.grades = grades;
}

public int getStudentID() {
return studentID;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public String getFullName() {
return firstName + lastName;
}

public String getEmail() {
return email;
}

public int getAge() {
return age;
}

public int[] getGrades() {
return grades;
}

public int getAverageGrade() {
int averageGrade = 0;
for (int grade : grades) {
averageGrade += grade;
}
return averageGrade;
}
public void print() {
System.out.println(
"Student Name:\t" + getFullName() + "\n " +
"Student ID:\t" + getStudentID() + "\n" +
"Email:\t" + getEmail() + "\n" +
"Age:\t" + getAge() + "\n" +
"Average Grade:\t" + getAverageGrade()
);

}


}

最佳答案

我对您的代码进行了以下更改:

  • ArrayList 应存储 Student 类型的对象。 studentInfo 不引用任何已定义的类。
  • 在方法 add 中,将 newStudent 的类型更改为 Student
  • removeprint_allprint_invalid_emails 方法的 for 循环中进行相同的必要调整。

更正后的代码如下所示:

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Roster
{
private static ArrayList<Student> studentList = new ArrayList<>();

public static void main(String[] args)
{

add(1, "John", "Smith", "John1989@gmail.com", 20, 88, 79, 59);
add(2, "Suzan", "Erickson", "Erickson_1990@gmailcom", 19, 91, 72, 85);
add(3, "Jack", "Napoli", "The_lawyer99yahoo.com", 19, 85, 84, 87);
add(4, "Erin", "Black", "Erin.black@comcast.net", 22, 91, 98, 82);
print_all();
print_invalid_emails();
print_average_grade(2);
remove(3);
remove(3);
}

public static void add (int studentID, String firstName, String lastName, String email, int age, int grade1, int grade2, int grade3)
{
int[] grades = {grade1, grade2, grade3};
Student newStudent = new Student (studentID, firstName, lastName, email, age, grades);
studentList.add(newStudent);
}

public static void remove(int studentID)
{
for (Student i: studentList) {
if (i.getStudentID() == studentID) {
studentList.remove(i);
System.out.println("Student Removed");
}
else {
System.out.println("A student with this ID(studentID) was not found.");
return;
}

}
}
public static void print_all (){
for (Student i: studentList) {
i.print();
}
}
public static void print_average_grade(int studentID) {
studentList.get(studentID).getAverageGrade();
}

public static void print_invalid_emails() {
String RFC5322 = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$";
Pattern pattern = Pattern.compile(RFC5322);
for (Student i: studentList) {
String email = i.getEmail();
Matcher matcher = pattern.matcher(email);
System.out.println(email +":\t"+ matcher.matches());
}

}


}

关于java - ArrayList 错误 - 找不到符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43711144/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com