打印信息表

    xiaoxiao2025-06-25  3

    假设一个年级有40名学生,他们的学号分别从128480到1244840,他们全部人都参加学校的化学,历史,地理,物理测试。下面将他们的全部成绩通过随机生成的方法进行打印出来,同时还要按顺序从大到小的排序出来。 具体的制作方法,要调用到多方面的元素来运行。如下图所示: public class Fill { public static void main(String[] args) throws IOException { //实例化列表 List list = new ArrayList<>(); //实例随机的参数值 Random random = new Random(); for (int i = 1; i < 41; i++) { //定义两个接受参数传过来的数值 String str=""; String str2=""; if (i<10) { str=“128480”+i; str2=“同学0”+i; }else{ str=“12848”+i; str2=“同学”+i; } //定义随机生成的成绩 int run1 = random.nextInt(100); int run2 = random.nextInt(100); int run3 = random.nextInt(100); int run4 = random.nextInt(100); //总成绩扽与其他成绩加起来的总数 int run5 = run1+run2+run3+run4; //用If…else来对循环进行条件判断 if (run1>50) { if (run2>50) { if(run3>50){ if(run4>50){ list.add(new Pig(str,str2, run1, run2, run3, run4,run5)); }else{ //循环完之后对参数进行-参数值 i=i-1; } }else{ i=i-1; } }else{ i=i-1; } } else{ i=i-1; } } //调用实例化的表格数据 Collections.sort(list); //利用迭代器输出结果 Iterator it= list.iterator(); //while循环输出 while (it.hasNext()) { Pig pig = (Pig) it.next(); System.out.println(“学校号:” + pig.getStudentNamber()+“学生名称:” + pig.getStudentName() + “历史分数:” + pig.getHistory() + “化学分数:” + pig.getGeography() + “地理分数:” + pig.getChemistry() + “物理分数:” + pig.getPhysics()+ “总分数:” + pig.getCount()); } } } 以下是定义整个程序的开发要点内容:(同时也是必须要定义的属性) //申明一个类来传参数 class Pig implements Comparable{ private String StudentNamber;//学生在校的编号 private String StudentName;//学生的姓名 //private String Student; private int Chemistry;//化学 private int History;//历史 private int Geography;//地理 private int Physics;//物理 private int Count;//学科的总分 //拼接所有的值 public Pig(String studentNamber, String studentName, int chemistry, int history, int geography, int physics, int count) { super();//指向的是自己本身的值 StudentNamber = studentNamber; StudentName = studentName; Chemistry = chemistry; History = history; Geography = geography; Physics = physics; Count = count; } //定义所有的数值应用对象 public String getStudentNamber() { return StudentNamber; } public void setStudentNamber(String studentNamber) { StudentNamber = studentNamber; } public String getStudentName() { return StudentName; } public void setStudentName(String studentName) { StudentName = studentName; } public int getChemistry() { return Chemistry; } public void setChemistry(int chemistry) { Chemistry = chemistry; } public int getHistory() { return History; } public void setHistory(int history) { History = history; } public int getGeography() { return Geography; } public void setGeography(int geography) { Geography = geography; } public int getPhysics() { return Physics; } public void setPhysics(int physics) { Physics = physics; } public int getCount() { return Count; } public void setCount(int count) { Count = count; } //定义排序数值的方法 @Override public int compareTo(Pig o) { // TODO Auto-generated method stub return o.getCount()-this.getCount(); } } 程序执行的结果:

    最新回复(0)