我正在为学校的最后一个项目工作,遇到了一些麻烦。我从未用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", "[email protected]", 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", "[email protected]", 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()
        );

    }


}

最佳答案

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


ArrrayList应该存储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", "[email protected]", 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", "[email protected]", 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());
     }

 }


}

09-30 14:42