# 急問一個JAVA程式的問題

……[共 20 筆]

……[共 20 筆]

Rating
• 9 years ago

請參考我的做法

import java.util.*;

class Pupil implements Comparable<Pupil> {

String name;

int id, s1, s2, s3, pre, pRank;

int pubK1, pubK2, priK1, priK2;

boolean[] pres = new boolean[18];

int[] pre2 = new int[pres.length];

boolean sortByID = false;

Pupil(String name, int id) {

this.name = name;

this.id = id;

for (int i = 0; i < pres.length; i++) {

pres[i] = (Math.random() > 0.3) ? true : false;

if (pres[i]) {

pre2[i] = 1;

pre++;

} else

pre2[i] = 0;

}

}

double avg() {

return (int)((s1 + s2 + s3) * 100 / 3.0D) / 100D;

}

public String toString() {

return "學生：" + name + "\t學號：000000" + id +

"\n各科成績：" + s1 + ", " + s2 + ", " + s3 + "\t平均成績：" + avg() +

"\n公開金鑰(" + pubK1 + ", " + pubK2 + ") 私密金鑰（" + priK1 + ", " + priK2 + ")" +

"\n點名資料" + Arrays.toString(pre2);

}

public int compareTo(Pupil o) {

if (sortByID) return id - o.id;

return o.pre - pre;

}

}

public class Y1299 {

public static void main(String[] args) {

Pupil[] ps = new Pupil[20];

for (int i = 0; i < ps.length; i++) {

Pupil p = new Pupil(i + "先生", i);

p.s1 = (int)(Math.random() * 100);

p.s2 = (int)(Math.random() * 100);

p.s3 = (int)(Math.random() * 100);

System.out.println(p);

ps[i] = p;

}

Arrays.sort(ps);

for (int i = 0; i < ps.length; i++) {

ps[i].pRank = i * 100 / ps.length;

ps[i].sortByID = true;

}

Arrays.sort(ps);

for (Pupil p : ps) {

System.out.println(p);

System.out.print("總共出席" + p.pre + "次，班上出勤排名：" + p.pRank + "%");

if (p.pRank <= 20) {

System.out.println("，加分 10%");

System.out.println("加分後成績：" + p.avg() * 1.10D);

} else if (p.pRank <= 50) {

System.out.println("，加分 5%");

System.out.println("加分後成績：" + p.avg() * 1.05D);

} else if (p.pRank > 85) {

System.out.println("，扣分 5%");

System.out.println("扣分後成績：" + p.avg() * 0.95D);

} else System.out.println();

}

System.out.println("......[共" + ps.length + "茟]");

}

}