0

请问我能得到一些帮助吗?我不知道如何让我的 If 语句搜索学生工作。我收到一个错误:

incompatible types
found   : Student
required: boolean
if (search(name, students))

另外,我对如何实现冒泡排序一无所知。

public class StudentProcessor2 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        // read in how many students to process
        System.out.print("How many students?");
        int numStudents = input.nextInt();

        Student[] students = new Student[numStudents];

        for (int i = 0; i < numStudents; i++) {
            // read in student data from Scanner
            System.out.println("What is the students name?");
            String name = input.next();

            System.out.println("What is the students grade?");
//          System.out.println("***Type -1 if no more grades***");
            int grade = input.nextInt();
            Student student = new Student();
            student.setName(name);
            student.setGrade(grade);
            students[i] = student;
        }
        UserSelection(input, numStudents, students);
    }

    // show menu with options
    public static void UserSelection(Scanner input, int numStudents,
            Student[] students) {
        // Repeatedly process menu selections by the user.
        int choice;
        do {
            System.out.println();
            System.out.println("*** Student Exam Results Menu ***");
            System.out.println("(1) Display the results");
            System.out.println("(2) Display the average result");
            System.out.println("(3) Display the highest grade");
            System.out.println("(4) Display the lowest grade");
            System.out.println("(5) Search for specific result");
            System.out.println("(6) Search for student grade by name");
            System.out.println("(7) Sort the results in ascending order");
            System.out.println("(8) quit");
            choice = input.nextInt();

            if (choice == 1)
            {
                System.out.println(Arrays.toString(students));
            } else if (choice == 2)

            {
                double average = getAverage(students);
                System.out.println("average grade = " + average);
            } else if (choice == 3)

            {
                int high = getHighest(students);
                System.out.println("high grade = " + high);
            } else if (choice == 4)

            {
                int low = getLowest(students);
                System.out.println("low grade = " + low);

            } else if (choice == 5)

            {
                System.out.print("Result to look for: ");
                int grade = input.nextInt();
                 if (result(grade, students)) {
                 System.out.println(grade +
                 " is in the collection of grades.");
                 } else

                 {
                 System.out.println(grade +
                 " is not in the collection of grades.");
                 }

            } else if (choice == 6)

            {
                System.out.print("Student to search for: ");
                String name = input.nextLine();
                if (search(name, students))
                {
                    System.out.println(name +
                    " is in the list of Students.");
                } else
                {
                System.out.println(name +
                " is not in the list of Students");
                }
            }

        } while (choice != 8);
    }

    // get Lower Grade
    private static int getLowest(Student[] students) {
        int lowest = 100;
        Student result = null;
        for (Student student : students) {
            if (student.getGrade() < lowest) {
                lowest = student.getGrade();
                result = student;
            }
        }
        return result.getGrade();
    }

    // get Highest grade
    private static int getHighest(Student[] students) {
        int highest = 0;
        Student result = null;
        for (Student student : students) {
            if (student.getGrade() > highest) {
                highest = student.getGrade();
                result = student;
            }
        }
        return result.getGrade();
    }

    // get Average grade
    private static double getAverage(Student[] students) {
        int total = 0;
        for (Student student : students) {
            total += student.getGrade();
        }
        return total / students.length;
    }

    // Search for student
    private static Student search(String name, Student[] students) {
        Student result = null;
        for (Student student : students) {
            if (student.getName().equalsIgnoreCase(name)) {
                result = student;
                break;
            }
        }
        return result;
    }
    // Search for grade
    private static boolean result(int grade, Student[] students) {
        for (Student student : students) {
            if (student.getGrade() == grade) {
                return true;
            }
        }
        return false;
    }

    // !Bubble sort goes here!
    private void sort(Student[] students) {

        Student hold; // temporary holding area for swap

        for (int i = 0; i < students.length; i++) { // passes
            Student current = students[i];
            Student next = students[i + 1];
            if (current.getGrade() > next.getGrade()) // one comparison
            {
                hold = students[i]; // one swap
                students[i] = students[i + 1];
                students[i + 1] = hold;
            }
        }
    }
}
4

1 回答 1

0

您的search方法返回 aStudent Object以便您可以检查返回的实例。ifstatements 表达式必须是boolean表达式,因此错误。代替

if (search(name, students))

if (search(name, students) == null)

Wrt冒泡排序,有很多可用的资源,比如这个

于 2013-03-19T21:28:20.480 回答