java习题1
题目:
建立一个学生类,有姓名,学号,3门课成绩,总分等信息,能输入输出学生数据,并能对总分进行排序,打印名次。
1 import java.lang.String; 2 import java.util.Scanner; 3 import java.util.Arrays; 4 import java.util.*; 5 //import java.lang.reflect; 6 class student implements Comparable 7 { 8 Scanner a=new Scanner(System.in); 9 String name; 10 int number; 11 double grade1,grade2,grade3; 12 double sum; 13 void get() 14 { 15 System.out.println("请输入学生姓名:"); 16 name=a.nextLine(); 17 System.out.println("请输入学生学号:"); 18 number=a.nextInt(); 19 System.out.println("请输入学生课程1成绩:"); 20 grade1=a.nextDouble(); 21 System.out.println("请输入学生课程2成绩:"); 22 grade2=a.nextDouble(); 23 System.out.println("请输入学生课程3成绩:"); 24 grade3=a.nextDouble(); 25 sum=grade1+grade2+grade3; 26 System.out.println("学生信息录入成功!"); 27 } 28 void put() 29 { 30 System.out.println("学生姓名"+name+" "+"学生学号"+number); 31 System.out.println("课程1"+" "+grade1); 32 System.out.println("课程2"+" "+grade2); 33 System.out.println("课程3"+" "+grade3); 34 System.out.println("总分"+" "+sum); 35 36 } 37 public int compareTo(Object obj) 38 { 39 student p=(student)obj; 40 if(p.sum>this.sum) 41 return 1; 42 else if(p.sum<this.sum) 43 return -1; 44 else {return 0;} 45 } 46 47 } 48 public class aa 49 { 50 public static void main(String []args) 51 { 52 student a[]=new student[3]; 53 for(int i=0;i<3;i++) 54 { 55 a[i]=new student(); 56 a[i].get(); 57 } 58 Arrays.sort(a); 59 for(int i=0;i<3;i++) 60 a[i].put(); 61 62 63 64 } 65 66 67 }