2013-03-20 117 views
-1

有人可以幫助我得到這個冒泡排序工作嗎?我是一名Java初學者,現在從未使用排序或數組。我還需要使用菜單中的if語句進行初始化。Java冒泡排序不起作用

冒泡排序方法:

private void sort(int grade, Student[] students) { 

    int temp = 0; // temporary holding area for swap 
    int i, j; 

    if (students.length < 2) {return;} 
    // Loop through length of the array 
    for (Student student : students) { 
    for (i = 0; i < students.length; i++) { 
    // Check to see if there is anything smaller and replace 
     for (j = i+1; j < students.length - 1; j++) { 
      if (student[i] > student[j]) 
      { 
       temp = student[i]; 
       student[i] = student[j]; 
       student[j] = temp; 

菜單迄今:(需要添加一個選項7冒泡排序工作?)

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) ==null) 
       { 
        System.out.println(name + 
        " is in the list of Students."); 
       } else 
       { 
       System.out.println(name + 
       " is not in the list of Students"); 
       } 
      } 
+0

你的排序功能不全出現。 – 2013-03-20 11:38:23

+0

你不需要'for(學生student:學生){' – Siddharth 2013-03-20 11:40:12

+0

'而不是'student [i]',使用'students [i]'。 – 2013-03-20 11:43:39

回答

1

Strudent類實現ComparablecompareTo方法的執行只比較name屬性,如果您需要其他屬性進行比較,您應該將它們包含在此方法中。

public class Student implements Comparable<Student> { 
    private String name; 
    private int grade; 
    public String getName() { 
    return name; 
    } 
    public void setName(String name) { 
    this.name = name; 
    } 
    public int getGrade() { 
    return grade; 
    } 
    public void setGrade(int grade) { 
    this.grade = grade; 
    } 
    public int compareTo(Student o) { 
    return this.getName().compareTo(o.getName()); 
    } 
} 

要實現排序使用

//Sort 
    public static void sort(Student[] students) 
    { 
    int j; 
    boolean flag = true; // set flag to true to begin first pass 
    Student temp; //holding variable 

    while (flag) 
    { 
     flag = false; //set flag to false awaiting a possible swap 
     for (j = 0; j < students.length -1; j++) 
     { 
     if (students[j].compareTo(students[j+1]) < 0) // change to > for ascending sort 
     { 
      temp = students[j]; //swap elements 
      students[j] = students[j+1]; 
      students[j+1] = temp; 
      flag = true; //shows a swap occurred 
     } 
     } 
    } 
    } 
+0

@kyub我已經告訴你如何做到這一點。 – 2013-03-20 13:08:58

+0

感謝您的幫助,這是完美的!我怎麼能在我與If語句菜單包括這個...像否則,如果(選擇== 7) \t \t \t { \t \t \t \t的System.out.println(「升序排列的結果是: 「+排序); \t \t \t} – kyub 2013-03-20 13:15:56