51,408
社区成员
发帖
与我相关
我的任务
分享
package homework;
import java.util.Scanner;
public class Student {
public static Student[] studentArray = new Student[3];
public static float[] mul = new float[3];
public static String name;
public static int number;
public static String sex;
public static int Cscore;
public static int Mscore;
public static int Escore;
public static int Ccredit;// 学分
public static int Mcredit;
public static int Ecredit;
public static int index = 0;
public static float code;
public static String flag;
public Student(String name, int number, String sex, int Cscore, int Mscore, int Escore, int Ccredit, int Mcredit,
int Ecredit) {
this.name = name;
this.number = number;
this.sex = sex;
this.Cscore = Cscore;
this.Ccredit = Ccredit;
this.Mscore = Mscore;
this.Mcredit = Mcredit;
this.Escore = Escore;
this.Ecredit = Ecredit;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
// Scanner input = new Scanner(System.in);
System.out.println("请输入学生姓名");
name = input.next();
System.out.println("请输入学号");
number = input.nextInt();
System.out.println("请输入性别");
sex = input.next();
System.out.println("请输入语文成绩");
Cscore = input.nextInt();
System.out.println("请输入语文学分");
Ccredit = input.nextInt();
System.out.println("请输入数学成绩");
Mscore = input.nextInt();
System.out.println("请输入数学学分");
Mcredit = input.nextInt();
System.out.println("请输入英语成绩");
Escore = input.nextInt();
System.out.println("请输入英语学分");
Ecredit = input.nextInt();
Student st = new Student(name, number, sex, Cscore, Mscore, Escore, Ccredit, Mcredit, Ecredit);//创建对象
studentArray[index++] = st;//将对象存入数组
}
for (int k = 0; k < 3; k++) {
System.out.println("姓名:" + studentArray[k].name + "\t学号:" + studentArray[k].number + "\t性别:"
+ studentArray[k].sex + "\t语文成绩:" + studentArray[k].Cscore + "\t语文学分" + studentArray[k].Ccredit
+ "\t数学成绩:" + studentArray[k].Mscore + "\t数学学分" + studentArray[k].Mcredit + "\t英语成绩:"
+ studentArray[k].Escore + "\t英语学分" + studentArray[k].Ecredit);
}
System.out.println("成绩排名如下:");
for (int j = 0; j < 3; j++) {
mul[j] = (float) (studentArray[j].Cscore * studentArray[j].Ccredit
+ studentArray[j].Mscore * studentArray[j].Mcredit
+ studentArray[j].Escore * studentArray[j].Ecredit)
/ (studentArray[j].Ccredit + studentArray[j].Mcredit + studentArray[j].Ecredit);
int minIndex = j;
for (int p = j; p < mul.length; p++) {
if (mul[p] > mul[minIndex])// 找到最小的数
minIndex = p;// 将最小数的索引保存
}
flag = studentArray[minIndex].name;
studentArray[minIndex].name = studentArray[j].name;
studentArray[j].name = flag;
code = mul[minIndex];
mul[minIndex] = mul[j];
mul[j] = code;
System.out.println("姓名:" + studentArray[j].name + ",加权平均分:" + mul[j]);
}
}
}