2012-03-19 43 views
1

我很抱歉問,但我在書中練習時遇到了問題,而且我不確定如何解決此問題。輸入學生的名字和成績後,我會找到最高和第二高的分數。但是我找不到找到兩個最高分的正確方法。對數組中的兩個最高數字進行排序

我使用的作品目前的方式,但是用戶輸入的分數從低到高,例如70,80和90.如果完成90,80和70,它會對數字進行適當排序。

有什麼我可以改變/做/讀讓我在正確的道路上?

import java.util.Scanner; 

public class StudentSort { 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 

     // For finding highest scores with corresponding array 
     double firstHighest = 0; 
     int firstEntry = 0; 
     double secondHighest = 0; 
     int secondEntry = 0; 

     System.out.print("Enter the number of students: "); 
     int studentCount = input.nextInt(); 

     // Length of arrays set 
     int[] studentScores = new int[studentCount]; 
     String[] studentName = new String[studentCount]; 

     // Go through loop to set scores and names of each student 
     for (int i = 0; i < studentCount; i++) { 
      System.out.print("Enter a student name: "); 
      studentName[i] = input.next(); 
      System.out.print("Enter a student score: "); 
      studentScores[i] = input.nextInt(); 
      } 

     // Find out the highest and second highest scores 
     // Problem with secondHighest/Entry 
     for (int i = 0; i < studentScores.length; i++) { 
      if (studentScores[i] > firstHighest) { 
       secondHighest = firstHighest; 
       firstHighest = studentScores[i]; 
       firstEntry = i; 
     } else if (studentScores[i] > secondHighest) { 
       secondHighest = studentScores[i]; 
       secondEntry = i; 
     } 
    } 

    System.out.println("Top two students: "); 
    System.out.println(studentName[firstEntry] + "'s score is " + firstHighest); 
    System.out.println(studentName[secondEntry] + "'s score is " + secondHighest); 
    } 
} 

與往常一樣,我感謝您提供任何幫助。

+0

我建議使用調試器在程序進行時檢查變量的值,或者添加一個'println'語句來顯示這些中間值。通過這種方式,您可以比較您期望發生的情況和實際發生的情況。 – 2012-03-19 20:28:57

+0

什麼不正確? (錯誤的輸出,錯誤信息,...) – talnicolas 2012-03-19 20:29:38

+0

只是按降序排列你的數組,並選擇前兩個條目(對不起,我處於「Captain Obvious」心情) – 2012-03-19 20:29:41

回答

4

的問題是在這裏

if (studentScores[i] > firstHighest) { 
     secondHighest = firstHighest; 
     firstHighest = studentScores[i]; 
     firstEntry = i; 
    } 

您成功更新都secondHighest和firstHighest的值,但你不解決seco​​ndEntry;

你需要添加

secondEntry = firstEntry; 

firstEntry = i; 
+0

這已經完全解決了這個問題。 非常感謝。 – Battleroid 2012-03-19 20:33:47

+1

@CaseyWeed不客氣 – twain249 2012-03-19 20:34:21

5

看起來你忘了在獲得新的最高分時更新secondEntry。行前:

  firstEntry = i; 

嘗試增加:

  secondEntry = firstEntry; 
0

您可以簡單地使用java.util.Arrays類及其sort()方法。以下是代碼的樣子:

Arrays.sort(studentScores); 
int firstHighest = studentScores[studentScores.length - 1]; 
int secondHighest = studentScores[studentScores.length - 2]; 

希望這會有所幫助。

+0

但是之後他會輸掉比分名稱。 – 2012-03-19 20:35:18

0

我給你寫了一個內部類的幫助方法;它會返回一個數組,第一個元素是學生的最高分數,第二個學生是第二個分數的學生。

import java.util.Arrays; 
import java.util.Comparator; 

public class StudentSort { 

static class Student { 
     Student(int score, String name) { 
     this.score = score; 
     this.name = name; 
     } 
     int score; 
     String name; 
    } 

    private static Student[] test(int[] studentScores, String[] studentNames) { 
     Student[] students = new Student[studentScores.length]; 
     for(int i = 0; i < studentScores.length; i++) { 
     students[i] = new Student(studentScores[i], studentNames[i]); 
     } 
     Arrays.sort(students, new Comparator<Student>() { 
     @Override 
     public int compare(Student o1, Student o2) { 
      return new Integer(o1.score).compareTo(o2.score); 
     } 
    }); 
     return new Student[]{students[0], students[1]}; 
    } 

    public static void main(String[] args) { 
     int[] studentScores = new int[] {5, 9, 7}; 
     String[] studentNames = new String[]{"Jan", "Bert", "Piet"}; 
     Student[] students = test(studentScores, studentNames); 
     System.out.println("heighest: " + students[0].name + ": " + students[0].score); 
     System.out.println("second: " + students[1].name + ": " + students[1].score); 
    } 
} 
1

有一種以上的方法可以解決這個問題。最直觀的方式類似於拿起一張牌(即插入排序)。將輸入視爲連續的卡片列表。他們進來的順序並不重要,因爲大多數球員會將他們排序從最低到最高(或其他方式)。

while(...some_condition_that_ensures_more_input){ 
    //this can be a list for instance, which you keep in order 
    list = insert_into_correct_place(input);            
} 

/** 
    Assuming 
    a)you sort from lowest to highest 
    b)there is more than 1 input entered): 
*/ 
highest = list.get(list.length()-1) 
second_highest = list.get(list.length()-2) 

另一種直觀的方式(實際上是更快)的辦法是隻保留兩個變量的軌跡:

int [] highest = {Integer.MIN_VALUE(), Integer.MIN_VALUE()};  
while(...){ 
     highest = replace_lowest(input, highest); 
} 

/** 
    * arr is sorted from lowest to highest : arr[0] is always <= arr[1] 
    */ 
int [] replace_lowest(int input, int [] arr){   
    //Case 0 : input is less than both the highest 2 numbers 
    //   or is equal to one of them 
    if (input < arr[0] || input == arr[0] || input == arr[1]) { return arr; } 

    //Case 1 : input is greater than one of the highest 2, but not both 
    if (input > arr[0] && input < arr[1]) { arr[0] = input; return arr; } 

    //Case 2 : input is greater than both of the highest 2 numbers 
    second_highest = arr[1]; arr[0] = arr[1]; arr[1] = input; 
    return arr;  
} 

第一

在您輸入迴路

所以方法更靈活一點(它可以讓你選出X個最高的數字而不是兩個)。如果您知道不必重新調整輸出變量的數量,第二種方法會更快。