m0_74423385 2024-04-13 20:08 采纳率: 60%
浏览 15
已结题

优秀学生的问题 oj java

题目描述
学校准备对优秀学生进行表彰,优秀学生包括本科生与研究生。本科生评优的要求是必须过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

我不要新的答案,我只要在我的基础上修改的答案

  • 写回答

18条回答 默认 最新

  • micthis 2024-04-14 10:21
    关注

    1、
    排序有问题,你的选择排序是按学年综合评测成绩升序排序的,应该按降序排序。

    if(stu[i].comtest>stu[j].comtest) {
    改成
    if(stu[i].comtest<stu[j].comtest) {
    2、
    你的学号是作为字符串比较大小的,如果学号不是等长的,结果会不对,比如"2"与"12",看你的学号的输入格式而定,看是否需要转换成Long再比较。
    试试:

    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) {
                    //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&&Long.valueOf(stu[i].sno).compareTo(Long.valueOf(stu[j].sno))>0) {
                    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);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(17条)

报告相同问题?

问题事件

  • 系统已结题 4月23日
  • 已采纳回答 4月15日
  • 修改了问题 4月13日
  • 创建了问题 4月13日

悬赏问题

  • ¥15 RPG游戏架构设计和开发方法
  • ¥15 python 计算股权结构
  • ¥30 为什么会失败呢,该如何调整
  • ¥15 前端返回pdf时不显示内容
  • ¥50 如何在不能联网影子模式下的电脑解决usb锁
  • ¥20 服务器redhat5.8网络问题
  • ¥15 如何利用c++ MFC绘制复杂网络多层图
  • ¥20 要做柴油机燃烧室优化 需要保持压缩比不变 请问怎么用AVL fire ESE软件里面的 compensation volume 来使用补偿体积来保持压缩比不变
  • ¥15 python螺旋图像
  • ¥15 算能的sail库的运用