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

コードの何が問題なのかわかりません

コンパイルしようとすると、これら3つのことでエラーが発生します

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

Major は変数ではなく、メジャーです。

2番:

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

また、次のようにすると、実行時にエラーが発生します

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 に答える