定義一個Student陣列,其中保存學生的基本資訊,包括姓名、學號、性別,還分別保存三門課程的成績及三門課程對應的學分。試編程計算這三門課程的學分積,并按學分積的降冪進行排序,輸出排序后的結果。
uj5u.com熱心網友回復:
定義一個學生類,包含學生基本資訊和成績學分屬性.定義一個學生型別的陣列,new幾個學生進去.
遍歷獲取這個陣列每個學生,然后取出屬性計算,排序.輸出
uj5u.com熱心網友回復:
可以編一下代碼嗎,謝謝
uj5u.com熱心網友回復:
public class Day05152019 {
public static void main(String[] args) {
Student1 s[] = new Student1[3];
s[0] = new Student1("小王", "001", "男", 20.0, 2.5, 80.0, 5.0, 68.0, 4.5);
s[1] = new Student1("小張", "002", "男", 50.0, 2.5, 60.0, 5.0, 78.0, 4.5);
s[2] = new Student1("小李", "003", "男", 30.0, 2.5, 50.0, 5.0, 88.0, 4.5);
Map<Double, Student1> map = new TreeMap();
for (int i = 0; i < s.length; i++) {
double v = s[i].getACredit() * s[i].getAScore() + s[i].getBCredit() * s[i].getBScore() + s[i].getCCredit() * s[i].getCScore();
map.put(v, s[i]);
}
Iterator<Double> it = map.keySet().iterator();
while (it.hasNext()) {
Double next = it.next();
System.out.println(next + " aaaaa " + map.get(next).toString());
}
}
}
class Student1 {
//包括姓名、學號、性別,還分別保存三門課程的成績及三門課程對應的學分
private String name;
private String id;
private String sex;
private Double AScore;
private Double ACredit;
private Double BScore;
private Double BCredit;
private Double CScore;
private Double CCredit;
public Student1(String name, String id, String sex, Double AScore, Double ACredit, Double BScore, Double BCredit, Double CScore, Double CCredit) {
this.name = name;
this.id = id;
this.sex = sex;
this.AScore = AScore;
this.ACredit = ACredit;
this.BScore = BScore;
this.BCredit = BCredit;
this.CScore = CScore;
this.CCredit = CCredit;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Double getAScore() {
return AScore;
}
public void setAScore(Double AScore) {
this.AScore = AScore;
}
public Double getACredit() {
return ACredit;
}
public void setACredit(Double ACredit) {
this.ACredit = ACredit;
}
public Double getBScore() {
return BScore;
}
public void setBScore(Double BScore) {
this.BScore = BScore;
}
public Double getBCredit() {
return BCredit;
}
public void setBCredit(Double BCredit) {
this.BCredit = BCredit;
}
public Double getCScore() {
return CScore;
}
public void setCScore(Double CScore) {
this.CScore = CScore;
}
public Double getCCredit() {
return CCredit;
}
public void setCCredit(Double CCredit) {
this.CCredit = CCredit;
}
@Override
public String toString() {
return "Student1{" +
"name='" + name + '\'' +
", id='" + id + '\'' +
", sex='" + sex + '\'' +
", AScore=" + AScore +
", ACredit=" + ACredit +
", BScore=" + BScore +
", BCredit=" + BCredit +
", CScore=" + CScore +
", CCredit=" + CCredit +
'}';
}
}
uj5u.com熱心網友回復:
public class Student {/**成員變數:姓名,學號,性別,數學,英語,語文,數學學分,英語學分,語文學分,三門課程學分積
附加:按學分積降冪進行排序,輸出結果
*/
public float Math;//數學
public float English;//英語
public float Chinese;//語文
public int MathCredit;//數學學分
public int EnglishCredit;//英語學分
public int ChineseCredit;//語文學分
public float product;//學分積
public String name;
public String Id;
public String sex;
public static void main(String[] args) {//初始化陣列
Student stu1=new Student(); {
stu1.name="**";
stu1.Id="***";
stu1.sex="男";
stu1.Math=70;
stu1.MathCredit=5;
stu1.English=73;
stu1.EnglishCredit=4;
stu1.Chinese=83;
stu1.ChineseCredit=3;
stu1.product=stu1.Math*stu1.MathCredit+stu1.English*stu1.EnglishCredit+stu1.Chinese*stu1.ChineseCredit;
System.out.println("姓名 "+stu1.name);
System.out.println("學號 "+stu1.Id);
System.out.println("性別 "+stu1.sex);
System.out.println("數學成績 "+stu1.Math);
System.out.println("數學學分 "+stu1.MathCredit);
System.out.println("英語成績 "+stu1.English);
System.out.println("英語學分 "+stu1.EnglishCredit);
System.out.println("物理成績 "+stu1.Chinese);
System.out.println("物理學分 "+stu1.ChineseCredit);
System.out.println("學分積 "+stu1.product);
}
Student stu2=new Student();{
stu2.name="***";
stu2.Id="201730134002";
stu2.sex="男";
stu2.Math=73;
stu2.MathCredit=5;
stu2.English=72;
stu2.EnglishCredit=4;
stu2.Chinese=78;
stu2.ChineseCredit=3;
stu2.product=stu2.Math*stu2.MathCredit+stu2.English*stu2.EnglishCredit+stu2.Chinese*stu2.ChineseCredit;
System.out.println("姓名 "+stu2.name);
System.out.println("學號 "+stu2.Id);
System.out.println("性別 "+stu2.sex);
System.out.println("數學成績 "+stu2.Math);
System.out.println("數學學分 "+stu2.MathCredit);
System.out.println("英語成績 "+stu2.English);
System.out.println("英語學分 "+stu2.EnglishCredit);
System.out.println("物理成績 "+stu2.Chinese);
System.out.println("物理學分 "+stu2.ChineseCredit);
System.out.println("學分積 "+stu2.product);
}
Student stu3=new Student();{
stu3.name=***";
stu3.Id="201730134031";
stu3.sex="女";
stu3.Math=75;
stu3.MathCredit=5;
stu3.English=85;
stu3.EnglishCredit=4;
stu3.Chinese=86;
stu3.ChineseCredit=3;
stu3.product=stu3.Math*stu3.MathCredit+stu3.English*stu3.EnglishCredit+stu3.Chinese*stu3.ChineseCredit;
System.out.println("姓名 "+stu3.name);
System.out.println("學號 "+stu3.Id);
System.out.println("性別 "+stu3.sex);
System.out.println("數學成績 "+stu3.Math);
System.out.println("數學學分 "+stu3.MathCredit);
System.out.println("英語成績 "+stu3.English);
System.out.println("英語學分 "+stu3.EnglishCredit);
System.out.println("物理成績 "+stu3.Chinese);
System.out.println("物理學分 "+stu3.ChineseCredit);
System.out.println("學分積 "+stu3.product);
}
Student stu4=new Student();{
stu4.name="***";
stu4.Id="201730134047";
stu4.sex="男";
stu4.Math=90;
stu4.MathCredit=5;
stu4.English=90;
stu4.EnglishCredit=4;
stu4.Chinese=86;
stu4.ChineseCredit=3;
stu4.product=stu4.Math*stu4.MathCredit+stu4.English*stu4.EnglishCredit+stu4.Chinese*stu4.ChineseCredit;
System.out.println("姓名 "+stu4.name);
System.out.println("學號 "+stu4.Id);
System.out.println("性別 "+stu4.sex);
System.out.println("數學成績 "+stu4.Math);
System.out.println("數學學分 "+stu4.MathCredit);
System.out.println("英語成績 "+stu4.English);
System.out.println("英語學分 "+stu4.EnglishCredit);
System.out.println("物理成績 "+stu4.Chinese);
System.out.println("物理學分 "+stu4.ChineseCredit);
System.out.println("學分積 "+stu4.product);
}
System.out.print("學分積依次為:"+stu1.product+" "+stu2.product+" "+stu3.product+" "+stu4.product);
Student students[]=new Student[4];{
students[0]=stu1;
students[1]=stu2;
students[2]=stu3;
students[3]=stu4;
}
System.out.println("學分積降冪排序為:"+"\n");
for(int i=0;i<students.length;i++) {
for(int j=i;j<students.length;j++) {
if(students[i].product<students[j].product){
float temp=0;
temp=students[i].product;
students[i].product=students[j].product;
students[j].product=temp;
}
}
System.out.println("學分積降冪排序的結果為:"+students[i].product);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/81185.html
標籤:Eclipse
