java 使用物件導向方式實現錄入學生資訊,取出成績最大值、最小值、平均值、對其進行排序,java物件導向
題目:
java 使用物件導向方式實現錄入學生資訊,取出成績最大值、最小值、平均值、對其進行排序
gitup源碼:
https://github.com/benxiaohai8888/Javase/blob/master/ObjectStudent.java
代碼:
import java.util.Scanner;public class ObjectStudent{private String name;//姓名private int age;//年齡private double score;//分數//定義全域的變數private Student[] students;public void setName(String name){this.name=name;}public String getName(){return name;}public void setAge(int age){this.age=age;}public int getAge(){return age;}public void setScore(double score){this.score=score;}public double getScore(){return score;}//初始化學生public void initStudent(){Scanner scanner=new Scanner(System.in);students=new Student[3];System.out.println("--------輸入3個學生的資訊--------");int j=1;for(int i=0;i<students.length;i++){Student stu=new Student();System.out.println("輸入第["+j+"]個學生的資訊:");System.out.println("姓名:");stu.setName(scanner.next());System.out.println("年齡:");stu.setAge(scanner.nextInt());System.out.println("分數:");stu.setScore(scanner.nextDouble());students[i]=stu;j++;}//列印輸出for(int a=0;a<students.length;a++){Student stu=students[a];System.out.println("第["+(a+1)+"]個學生的姓名:"+stu.getName());System.out.println("第["+(a+1)+"]個學生的年齡:"+stu.getAge());System.out.println("第["+(a+1)+"]個學生的分數:"+stu.getScore());System.out.println("----------------------------------------");}}//總分public double sumScore(){double sumScore=0.0;for(int a=0;a<students.length;a++){Student stu=students[a];sumScore+=stu.getScore();}return sumScore;}//平均分public double avgScore(){return sumScore()/students.length;}//最大值public double max(){double max=students[0].getScore();for(int a=0;a<students.length;a++){if(students[a].getScore()>max){max=students[a].getScore();}}return max;}//最大值的學生public Student maxStudent(){double max=students[0].getScore();int index=-1;for(int a=0;a<students.length;a++){if(students[a].getScore()>max){max=students[a].getScore();index=a;}}return students[index];}//最小值public double min(){double min=students[0].getScore();for(int a=0;a<students.length;a++){if(students[a].getScore()<min){min=students[a].getScore();}}return min;}//最小值的學生public Student minStudent(){double min=students[0].getScore();int index=-1;for(int a=0;a<students.length;a++){if(students[a].getScore()<min){min=students[a].getScore();index=a;}else{min=students[0].getScore();index=0;}}return students[index];}//排序public void sort(){Student st=new Student();for(int i=0;i<students.length-1;i++){for(int j=i+1;j<students.length-1;j++){if(students[i].getScore() > students[j].getScore()){st=students[i];students[i]=students[j];students[j]=st;}}}for(Student s: students){System.out.print("姓名:"+s.getName()+" ");System.out.print("年齡:"+s.getAge()+" ");System.out.print("分數:"+s.getScore()+" ");System.out.println();}}public static void main(String[] args){Student stu=new Student();//初始化學生stu.initStudent();//總分System.out.println("總分: "+stu.sumScore());//平均分System.out.println("平均分: "+stu.avgScore());//分數最大值System.out.println("最大值: "+stu.max());//分數最大值的學生資訊System.out.println("分數最大值的學生姓名: "+stu.maxStudent().getName());//分數最小值System.out.println("最小值: "+stu.min());//分數最小值的學生資訊System.out.println("分數最小值的學生姓名: "+stu.minStudent().getName());//排序System.out.println("排序結果; ");stu.sort();}}
結果:
查看評論