-3
import java.util.ArrayList;

import java.util.Scanner;

public class StudentList {
    public static void main(String[] args) {

        ArrayList<Student> students = new ArrayList<Student>();
        int choice;

        printMenu();

        do {
            Scanner input = new Scanner(System.in);
            choice = input.nextInt();

            switch (choice) {

            case 1:
                System.out.println("\nAdd a student\n");
                students.add(addStudent());
                printMenu();
                break;
            case 2:
                System.out.println("\nFind a student\n");
                findStudent(students);
                printMenu();
                break;
            case 3:
                System.out.println("\nDelete a student\n");
                displayAllStudents(students);
                printMenu();
                break;
            case 4:
                System.out.println("\nDispay all students\n");
                displayAllStudents(students);
                printMenu();
                break;
            case 5:
                System.out.println("\nDisplay the total number of students\n");
                studentSize(students);
                printMenu();
                break;
            case 6:
                System.out.println("\nGoodbye!\n");
                break;
            default:
                System.out
                        .println("\nYour choice," + choice + ", is invalid\n");
                break;
            }
        } while (choice != 6);
    }

    public static void printMenu() {
        System.out
                .println("\nPlease select from the following menu:\n"
                        + "\t1. Add a student\n" + "\t2. Find a student\n"
                        + "\t3. Delete a student\n"
                        + "\t4. Display all students\n"
                        + "\t5. Display the total number of students\n"
                        + "\t6. Exit\n");
        System.out.print("Your choice: ");
    }

    public static Student addStudent() {
        Scanner inputS = new Scanner(System.in);
        System.out.println("First Name:");
        String fName = inputS.nextLine();
        System.out.println("Last Name:");
        String lName = inputS.nextLine();
        System.out.println("Major Name:");
        String major = inputS.nextLine();
        System.out.println("Student Name:");
        Integer sNumber = inputS.nextInt();
        System.out.println("gpa:");
        double grade = inputS.nextDouble();
        return new Student(fName, lName, Major, sNumber, grade);
    }

    public static void displayAllStudents(ArrayList<Student> students) {
        for (Student s : students) {
            System.out.print(s.toString());
        }
    }

    public static void findStudent(ArrayList<Student> students) {
        Scanner inputN = new Scanner(System.in);
        String name = inputN.nextLine();
        for (Student s : students) {

            if (s.getFName().equals(name)) {
                System.out.println(s);
                break;
            }

        }

    }

    public static void deleteStudent(ArrayList<Student> students) {
        Scanner inputS = new Scanner(System.in);
        String sNum = inputS.next();
        for (Student s : students) {
            if (s.FName().equals(sNum)) {
                students.remove(s);
                break;
            }
        }
    }

    public static void studentSize(ArrayList<Student> students) {
        System.out.printf("Size: %\n", students.size());
    }
}

我无法弄清楚我的代码有什么问题

当我尝试编译时,它在这三件事上给了我一个错误

return new Student(fName, lName, Major, sNumber, grade);

if(s.FName().equals(sNum))

if(s.getFName().equals(name))
4

3 回答 3

2

第一的:

return new Student(fName, lName, Major, sNumber, grade);

专业不是一个变量,它的专业。

第二:

if(s.FName().equals(sNum))

它应该是

if(s.getFName().equals(sNum))

更新

public static Student addStudent()     {
    Scanner inputS = new Scanner(System.in);
    System.out.println("First Name:");
    String fName = inputS.nextLine();
    System.out.println("Last Name:");
    String lName = inputS.nextLine();
    System.out.println("Major Name:");
    String major = inputS.nextLine(); //variable name: major
    System.out.println("Student Name:");
    Integer sNumber = inputS.nextInt();
    System.out.println("gpa:");
    double grade = inputS.nextDouble();
    //variable sent: Major, it should be major
    return new Student(fName, lName, Major, sNumber, grade);
}
于 2012-05-24T04:54:53.357 回答
1

你在一个地方

s.FName()

在另一个

s.getFName()

此外,以下会在 rumtime 给你错误

for(Student s: students)
{
    if(s.FName().equals(sNum))
    {
        students.remove(s);
        break;
    }  
}
于 2012-05-24T04:54:09.457 回答
0

在查看对象时,您不能从数组中删除对象。你应该使用迭代器。像这样:

for (Iterator<Student> it = students.iterator(); it.hasNext(); )
        {
            Student student = (Student)it.next();
            if (!s.getFName().equals(sNum))
            {
                tmp.add(student);
            }

        lines.clear();
        for (Student t: tmp)
            students.add(t);
        tmp.clear();
        }
于 2012-05-24T05:00:44.743 回答