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
我怎樣才能從Total
Rank
?
澄清:你想根據平均值排序你的線? – 2012-02-07 12:26:43
@JuriGlass不,我想根據Total打印Rank列中的1,2。 假設A得到總數爲60,B得到總數爲83.所以'A''領域中的'Rank'應該打印2,在'B'領域應該打印1。 – Bishan 2012-02-07 12:35:03