题目描述
学校准备对优秀学生进行表彰,优秀学生包括本科生与研究生。本科生评优的要求是必须过CET4,学年综合测评成绩大于85分;研究生评优的标准是过CET6,已发表文章篇数大于等于1,学年综合测评成绩大于85分。要求输出符合要求的学生信息(本科生信息:学号,姓名,是否过CET4,学年综合测评成绩;研究生信息:学号,姓名,是否过CET6,发表文章篇数,学年综合测评成绩)。
在主类中实例化n个本科生与m个硕士生(n+m<=100),输出优秀本科生信息,再输出优秀研究生信息,不同类别学生按综合测评成绩降序排序,若成绩相同则按学号升序排序。
要求用多态来设计判断学生是否优秀。
输入
整数t(t为n与m的和),n个本科生信息,m个研究生信息,注意这些信息交错输入,输入信息中首位为1代表本科生,2代表研究生。
输出
输出优秀本科生信息,再输出优秀研究生信息,不同类别学生按综合测评成绩降序排序,若成绩相同则按学号升序排序。成绩保留3位小数。若相应类别优秀生不存在,则任何信息都不输出。
package test2;
import java.util.*;
public class Outstandingstudent {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
Student[] stu=new Student[100];
int n;
int a[]=new int[100];
n=reader.nextInt();
for(int i=0;i<n;i++)
{
a[i]=reader.nextInt();
if(a[i]==1) stu[i]=new Undergraduate(reader.next(),reader.next(),reader.next(),reader.nextDouble());
else stu[i]=new Graduate(reader.next(),reader.next(),reader.next(),reader.nextInt(),reader.nextDouble());
}
for(int i=0;i<n;i++) {
for(int j=i+1;j<n;j++) {
if(stu[i].comtest>stu[j].comtest) {
Student t=stu[i];
stu[i]=stu[j];
stu[j]=t;
int x=a[i];
a[i]=a[j];
a[j]=x;
}
else if(stu[i].comtest==stu[j].comtest&&stu[i].sno.compareTo(stu[j].sno)>0) {
Student w=stu[i];
stu[i]=stu[j];
stu[j]=w;
int y=a[i];
a[i]=a[j];
a[j]=y;
}
}
}
int p=0,q=0;
Student[] s1=new Student[100];
Student[] s2=new Student[100];
for(int i=0;i<n;i++) {
if(a[i]==1&&stu[i].test()==true)
{
s1[p]=stu[i];
p++;
}
else if(a[i]==2&&stu[i].test()==true) {
{
s2[q]=stu[i];
q++;
}
}
}
if(p>0) {
System.out.println("excellent undergraduate student:");
for(int i=0;i<p;i++)
{
s1[i].show();
}
}
if(q>0) {
System.out.println("excellent graduate student:");
for(int i=0;i<q;i++)
{
s2[i].show();
}
}
}
}
class Student{
double comtest ;
String sno,name,cet;
Student(String sno,String name,String cet,double comtest){
this.name=name;
this.sno=sno;
this.cet=cet;
this.comtest=comtest;
}
boolean test() {
return false;
}
void show() {
}
}
class Graduate extends Student{
//String cet;
int write;
double comtest;
Graduate(String sno, String name,String cet,int write,double comtest) {
super(sno, name, cet, comtest);
this.comtest =comtest;
this.write=write;
}
boolean test() {
if(cet.equals("true")&&comtest>85&&write>=1) return true;
else return false;
}
void show() {
System.out.printf("%s,%s,%s,%d,%.3f\n",sno,name,cet,write,comtest);
}
}
class Undergraduate extends Student{
double comtest;
Undergraduate(String sno, String name,String cet,double comtest) {
super(sno, name, cet, comtest);
this.comtest=comtest;
}
boolean test() {
if(cet.equals("true")&&comtest>85) return true;
else return false;
}
void show() {
System.out.printf("%s,%s,%s,%.3f\n",sno,name,cet,comtest);
}
}
为什么我在oj上运行答案错误%30
我不要新的答案,我只要在我的基础上修改的答案