2013-03-19 26 views
0

我可以得到一些帮助吗?我无法弄清楚如何让我的If语句搜索一个学生工作。我得到一个错误:搜索方法似乎不错,但不是我的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; 
      } 
     } 
    } 
} 

回答

0

search方法返回一个StudentObject,所以你可以检查返回的实例。 if语句表达式必须是boolean表达式,因此是错误。更换

if (search(name, students)) 

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

WRT冒泡排序,还有诸如this one提供大量资源。

+0

我知道这是直接在我头上的东西。谢谢你Reimeus!明天晚上我会回到这个课程,因为我有一门考试可以学习,但泡沫分类的任何一点都会很棒。 – kyub 2013-03-19 21:42:14