2012-02-07 81 views
0

我寫下面的代碼。獲得排名總計

import java.io.*; 

class Test { 

    public static void main(String args[]) throws IOException { 
     DataInputStream dis = new DataInputStream(System.in); 

     String name[] = new String[10]; 
     float s1[] = new float[10]; 
     float s2[] = new float[10]; 
     float s3[] = new float[10]; 
     float s4[] = new float[10]; 
     float tot[] = new float[10]; 
     float avrg[] = new float[10]; 

     float total = 0; 
     float avg = 0; 

     for (int i = 0; i < 2; i++) { 
      total = 0; 
      avg = 0; 

      System.out.println("Enter Name : "); 
      String j = dis.readLine(); 
      name[i] = j; 


      System.out.println("Enter Mark1 : "); 
      String y = dis.readLine(); 
      float x = Float.parseFloat(y); 
      s1[i] = x; 

      System.out.println("Enter Mark2 : "); 
      String n = dis.readLine(); 
      float m = Float.parseFloat(n); 
      s2[i] = m; 

      System.out.println("Enter Mark3 : "); 
      String v = dis.readLine(); 
      float u = Float.parseFloat(v); 
      s3[i] = u; 

      System.out.println("Enter Mark4 : "); 
      String a = dis.readLine(); 
      float b = Float.parseFloat(a); 
      s4[i] = b; 

      total = x + m + u + b; 
      tot[i] = total; 
      avg = total/4; 
      avrg[i] = avg; 
     } 

     System.out.println(); 

     System.out.println("Name" + "\t" + "Sub_1" + "\t" + "Sub_2" + "\t" + "Sub_3" + "\t" + "Sub_4" + "\t" + "Total" + "\t" + "Avg" + "\t" + "Rank"); 
     System.out.println(); 

     for (int k = 0; k < 2; k++) { 
      System.out.println(name[k] + "\t" + s1[k] + "\t" + s2[k] + "\t" + s3[k] + "\t" + s4[k] + "\t" + tot[k] + "\t" + avrg[k]); 
     } 

     System.out.println(); 

    } 
} 

上面的代碼給出瞭如下結果。

Name Sub_1 Sub_2 Sub_3 Sub_4 Total Avg  Rank 

A  10.0 10.0 20.0 10.0 50.0 12.5 
B  20.0 20.0 30.0 10.0 80.0 20.0 

現在我想通過如下Total並打印輸出得到Rank

Name Sub_1 Sub_2 Sub_3 Sub_4 Total Avg  Rank 

A  10.0 10.0 20.0 20.0 60.0 15.0 2 
B  10.0 20.0 3.0  50.0 83.0 20.75 1 

我怎樣才能從TotalRank

+0

澄清:你想根據平均值排序你的線? – 2012-02-07 12:26:43

+0

@JuriGlass不,我想根據Total打印Rank列中的1,2。 假設A得到總數爲60,B得到總數爲83.所以'A''領域中的'Rank'應該打印2,在'B'領域應該打印1。 – Bishan 2012-02-07 12:35:03

回答

1

這會給你結果,但肯定這不是一個優化的解決方案。我剛剛給了一個工作解決方案。

import java.io.*; 

    class Test { 
    public static void main(String args[]) throws IOException { 
     DataInputStream dis = new DataInputStream(System.in); 
     String name[] = new String[10]; 
     float s1[] = new float[10]; 
     float s2[] = new float[10]; 
     float s3[] = new float[10]; 
     float s4[] = new float[10]; 
     float tot[] = new float[10]; 
     float avrg[] = new float[10]; 
     float rankAvg[] = new float[10]; 
     int rank[] = new int[10]; 
     float total = 0; 
     float avg = 0; 

     for (int i = 0; i < 4; i++) { 

      total = 0; 
      avg = 0; 
      System.out.println("Enter Name : "); 
      String j = dis.readLine(); 
      name[i] = j; 
      System.out.println("Enter Mark1 : "); 
      String y = dis.readLine(); 
      float x = Float.parseFloat(y); 
      s1[i] = x; 
      System.out.println("Enter Mark2 : "); 
      String n = dis.readLine(); 
      float m = Float.parseFloat(n); 
      s2[i] = m; 
      System.out.println("Enter Mark3 : "); 
      String v = dis.readLine(); 
      float u = Float.parseFloat(v); 
      s3[i] = u; 
      System.out.println("Enter Mark4 : "); 
      String a = dis.readLine(); 
      float b = Float.parseFloat(a); 
      s4[i] = b; 
      total = x + m + u + b; 
      tot[i] = total; 
      avg = total/4; 
      avrg[i] = avg; 
      rankAvg[i]= avrg[i]; 
     } 

     for(int i=0; i<4; i++){ 

      for(int j=i+1;j<4;j++){ 

       if(rankAvg[i] < rankAvg[j]){ 
        float t = rankAvg[j]; 
        rankAvg[j] = rankAvg[i]; 
        rankAvg[i] = t; 
       } 

      } 
     } 

     System.out.println(); 
     System.out.println("Name" + "\t" + "Sub_1" + "\t" + "Sub_2" + "\t" + "Sub_3" + "\t" + "Sub_4" + "\t" + "Total" + "\t" + "Avg" + "\t" + "Rank"); 
     System.out.println(); 

     for (int k = 0; k < 4; k++) { 

      System.out.println(name[k] + "\t" + s1[k] + "\t" + s2[k] + "\t" + s3[k] + "\t" + s4[k] + "\t" + tot[k] + "\t" + avrg[k] + "\t" + findRank(avrg, rankAvg, k)); 
     } 

     System.out.println(); 
    } 

    public static int findRank(float[] avrg, float[] rankAvg, int k){ 
     int rank=0; 
     for(int i=0; i<4; i++){ 
      if(avrg[k] == rankAvg[i]){ 
       rank=i+1; 
      } 
     } 
     return rank; 
    } 
}