import java.util.*;
import java.io.*;
import java.util.Scanner;
public class scholarship {
public static int numberOfStudent; //学生数目
public static String studentName; //学生姓名
public static int finalScore; //期末成绩
public static int compareScore; //评议成绩
public static char studentCadre; //学生干部
public static char westStudent; //西部学生
public static int numberOfPaper; //发表论文
public static int maxStudent = 0; //获奖最多的学生
public static final int academician = 8000; //院士奖学金
public static final int fivefour = 4000; //五四奖学金
public static final int excellent = 2000; //成绩优秀奖
public static final int west = 1000; //西部奖学金
public static final int contribution = 850; //班级贡献奖
public int personalScholarship = 0; //个人获得奖学金数目
public static int totalScholarship = 0; //奖学金总数
public static int numberOfSchStu = 0; //获奖学生数
public static int N;
public static void main (String[] agrs){
/**
* 输入部分
*/
Scanner reader = new Scanner(System.in);
System.out.println("输入学生数目:");
N = reader.nextInt();
scholarship [] sch = new scholarship[N];
int[] money = new int[N];
for(int i = 0;i < N;i ++){
sch[i].studentName = reader.next();
sch[i].finalScore = reader.nextInt();
sch[i].compareScore = reader.nextInt();
sch[i].studentCadre = reader.next().charAt(0);
sch[i].westStudent =reader.next().charAt(0);
sch[i].numberOfPaper = reader.nextInt();
/**
* 判断
*/
if((sch[i].finalScore > 80)&& (sch[i].numberOfPaper >= 1)){
// sch[i].personalScholarship = sch[i].personalScholarship + academician;
money[i] += academician;
totalScholarship += academician;
}
if((sch[i].finalScore > 85)&& (sch[i].compareScore >80)){
// sch[i].personalScholarship = sch[i].personalScholarship + fivefour;
money[i] += fivefour;
totalScholarship += fivefour;
}
if(sch[i].finalScore > 90){
// sch[i].personalScholarship = sch[i].personalScholarship + excellent;
money[i] += excellent;
totalScholarship += excellent;
}
if((sch[i].finalScore > 85)&& ((sch[i].westStudent == 'Y') ||(sch[i].westStudent == 'y'))){
// sch[i].personalScholarship = sch[i].personalScholarship + west;
money[i] += west;
totalScholarship += west;
}
if((sch[i].compareScore > 80)&& ((sch[i].studentCadre == 'Y') ||(sch[i].studentCadre == 'y'))){
// sch[i].personalScholarship = sch[i].personalScholarship + contribution;
money[i]+= contribution;
totalScholarship += contribution;
}
}
/*
* 找到单个学生获得的最高奖学金数目
*/
for(int i = 0;i < N;i ++){
if(money[i] > maxStudent){
maxStudent = money[i];
}
}
/*
* 冒泡算法排序
*/
for(int i = 0;i < N - 1;i ++){
for(int j = N - 1;j > i;-- j){
if(money[j] < money [j - 1]){
scholarship tmp = new scholarship();
tmp = sch[j];
sch[j] = sch[j - 1];
sch[j - 1] = tmp;
}
}
}
/**
* 输出
*/
System.out.println(sch[N - 1].studentName);
System.out.println(maxStudent);
System.out.println(totalScholarship);
}
}
样例输入:(正确的结果)
4
YaoLin 87 82 Y N 0
ChenRuiyi 88 78 N Y 1
LiXin 92 88 N N 0
ZhangQin 83 87 Y N 1
样例输出:
ChenRuiyi
9000
28700而我得到的获得最高奖学金的学生却总是最后一个输入的学生。如下:
输入学生数目:
4
yl 87 82 y n 0
cr 88 78 n y 1
lx 92 88 n n 0
zq 83 87 y n 1
zq
9000
28700
是排序没起作用么?有什么别的办法么?
新手求指教啊
得到money数组中的最大值
根据其索引便可得到获得最多奖学金的学生了
不用排序吧
类名首字母要大写
/*
* 找到单个学生获得的最高奖学金数目
*/
for(int i = 0;i < N;i ++){
if(money[i] > maxStudent){
maxStudent = money[i];
j = i;
}
}
然后在最后用sch[j]输出,但是也是一样的结果这样为什么也不行呢?
Java code
/*
* 找到单个学生获得的最高奖学金数目
*/
for(int i = 0;i < N;i ++){
if(money[i] > maxStudent){
maxStudent = money[i];
j = i;
}
}加了一句
然后在最后用sch[j]输出,但是也是一样的结果这样为什么也不行呢?