2015-02-08 39 views
-4

這裏是問題:數組排序 - 爪哇日食

國際奧委會已經要求你寫一個程序來處理數據,並確定了對獎牌獲得者花樣滑冰。您將得到以下文件: Pairs.txt

其中包含每對滑板的數據。數據包括每位選手的姓名,國家和八位評委在技術方面和成績方面的得分。一種典型的記錄將是如下:

史密斯 瓊斯 澳大利亞 5.0 4.9 5.1 5.2 5.0 5.1 5.2 4.8 4.3 4.7 4.8 4.9 4.6 4.8 4.9 4.5

每個溜冰者的最後得分是的總和這兩類分數的平均值。

設計一個類來保存上述數據和最終得分。從文件中讀取數據並計算每對的最終分數。對對象數組進行排序,並依次在屏幕上顯示結果,爲獎牌獲得者提供特別突出的參考。

這是我的課

public class Skaters { 

    private String name1; 
    private String name2; 
    private String country; 
    private double[] arrTech = new double[8]; 
    private double[] arrArt = new double[8]; 
    private double score; 

    public Skaters(String n1, String n2, String c1, double[] arrT, double[] arrA) 

    { 
     this.name1 = n1; 
     this.name2 = n2; 
     this.country = c1; 
     this.arrTech = arrT; 
     this.arrArt = arrA; 

    } 

    public void setName1(String n1) 

    { 
     name1 = n1; 
    } 

    public void setName2(String n2) 

    { 
     name2 = n2; 
    } 

    public void setCountry(String c1) 

    { 
     country = c1; 
    } 

    public void setArrTech(double[] arrT) 

    { 
     arrTech = arrT; 
    } 

    public void setArrArt(double[] arrA) 

    { 
     arrArt = arrA; 
    } 

    public String getName1() 

    { 
     return name1; 
    } 

    public String getName2() 

    { 
     return name2; 
    } 

    public String getCountry() 

    { 
     return country; 
    } 

    public double[] getArrTech() { 
     return arrTech; 
    } 

    public double[] getArrArt() { 
     return arrArt; 
    } 

    public double getScore() 

    { 
     double sum = 0, avg1, avg2; 

     for (int r = 0; r < arrTech.length; r++) 

      sum += arrTech[r]; 

     avg1 = sum/arrTech.length; 

     sum = 0; 

     for (int s = 0; s < arrArt.length; s++) 

      sum += arrArt[s]; 

     avg2 = sum/arrArt.length; 

     return (avg1 + avg2); 
    } 

} 

這是我的主要

import java.io.*; 
import java.util.Scanner; 

public class testSkaters { 

    private static final int NUMBER_OF_JUDGES = 8; 

    public static void main(String[] args) throws IOException { 

     Skaters[] arrSkaters = new Skaters[50]; 

     File myFile = new File("Pairs.txt"); 

     Scanner input = new Scanner(myFile); 

     int countSkaters = 0; 
     String name1; 
     String name2; 
     String country; 
     double[] arr1 = null; 
     double[] arr2 = null; 

     while (input.hasNext()) 

     { 

      name1 = input.nextLine(); 
      name2 = input.nextLine(); 
      country = input.nextLine(); 

      arr1 = new double[8]; 
      for (int i = 0; i < NUMBER_OF_JUDGES; i++) 

       arr1[i] = input.nextDouble(); 

      arr2 = new double[8]; 
      for (int j = 0; j < NUMBER_OF_JUDGES; j++) 

       arr2[j] = input.nextDouble(); 

      Skaters s1 = new Skaters(name1, name2, country, arr1, arr2); 

      arrSkaters[countSkaters] = s1; 

      countSkaters++; 

      input.nextLine(); 

     } 

     sortSkaters(arrSkaters, 10); 

     for (int t = 0; t < countSkaters; t++) 

     { 
      System.out.println("Name 1: " + arrSkaters[t].getName1()); 

      System.out.println("Name 2: " + arrSkaters[t].getName2()); 

      System.out.println("Country: " + arrSkaters[t].getCountry()); 

      printArray(arrSkaters[t].getArrTech()); 

      printArray1(arrSkaters[t].getArrArt()); 

      System.out.println("Score: " + arrSkaters[t].getScore() + "\n"); 

     } 

    } 

    public static void printArray(double[] arr3) { 
     for (int r = 0; r < arr3.length; r++) 

      System.out.print(arr3[r] + " "); 
     System.out.println(); 

    } 

    public static void printArray1(double[] arr4) { 
     for (int s = 0; s < arr4.length; s++) 

      System.out.print(arr4[s] + " "); 
     System.out.println(); 
    } 

    private static void sortSkaters(Skaters[] arr, double count) { 

     for (int i = 0; i < count; i++) { 
      int high = i; 

      for (int k = i + 1; k < count; k++) { 
       if (arr[k].getScore() > arr[high].getScore()) 

        high = k; 

       if (i != high) { 
        Skaters temp = arr[i]; 
        arr[i] = arr[high]; 
        arr[high] = temp; 
       } 

      } 

     } 
    } 
} 

這裏是文本文件

smith 
jones 
australia 
4.2 5.1 3.8 2.9 5.0 4.6 4.9 4.3 
4.9 4.8 5.8 3.8 4.9 4.6 5.0 4.5 
lennon 
murray 
england 
2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 
3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 
gusto 
petitot 
italia 
4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 
5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 
lahaie 
petit 
france 
1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 
5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 
bilodeau 
bernard 
canada 
2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 
4.1 4.2 4.3 4.8 4.9 4.6 4.0 4.5 
lahore 
pedro 
mexico 
3.2 3.1 3.8 3.9 3.0 3.6 3.9 3.3 
5.9 5.8 5.8 5.8 5.9 5.6 5.0 5.5 
maliak 
kolikov 
russia 
4.2 4.1 4.8 4.9 4.0 4.6 4.9 4.3 
1.9 1.8 1.8 1.8 1.9 1.6 1.0 1.5 
ching 
chong 
china 
5.2 5.1 5.8 5.9 5.0 5.6 5.9 5.3 
2.9 2.8 2.8 2.8 2.9 2.6 2.0 2.5 
gates 
brown 
U.S 
1.2 1.1 1.8 1.9 1.0 1.6 1.9 1.3 
3.9 3.8 3.8 3.8 3.9 3.6 3.0 3.5 
joe 
charlie 
switzerland 
2.2 2.1 2.8 2.9 2.0 2.6 2.9 2.3 
4.9 4.8 4.8 4.8 4.9 4.6 4.0 4.5 

我做了我的排序,但我的輸出是錯誤的。最高分應該是第一名(這是贏家等)。我的代碼不這樣做,任何人都可以幫我請

+0

Eclipse是一個IDE。如果你在java中對數組進行排序的方式與其他工具不同,那將會是個大問題。 – ChiefTwoPencils 2015-02-08 22:07:31

+0

您是否知道有構建數組的方法?你不必自己做。 – csmckelvey 2015-02-08 22:10:32

+0

@Takendarkk看起來像練習如何創建排序算法的功課。 – 2015-02-08 22:11:04

回答

0

這個問題似乎是在這裏:

private static void sortSkaters(Skaters[] arr, double count) { 
    for (int i = 0; i < count; i++) { 
     int high = i; 
     for (int k = i + 1; k < count; k++) { 
      if (arr[k].getScore() > arr[high].getScore()) 
       high = k; 
      //HERE 
      if (i != high) { 
       Skaters temp = arr[i]; 
       arr[i] = arr[high]; 
       arr[high] = temp; 
      } 
     } 
    } 
} 

if (i != high) {聲明在內for內的每個循環,當它應該在的唯一執行的處理外部for每個循環。只需將其移動到外部:

private static void sortSkaters(Skaters[] arr, double count) { 
    for (int i = 0; i < count; i++) { 
     int high = i; 
     for (int k = i + 1; k < count; k++) { 
      if (arr[k].getScore() > arr[high].getScore()) { 
       high = k; 
      } 
     } 
     //HERE 
     if (i != high) { 
      Skaters temp = arr[i]; 
      arr[i] = arr[high]; 
      arr[high] = temp; 
     } 
    } 
} 
+0

非常感謝你,它的工作 – 2015-02-22 15:03:05