java类,函数传参

 1 package 传参练习;
 2 //学生姓名组成的数组:指定区间和查找的名字返回此人是否存在(如果存在返回位置否则-1)
 3 public class test1 {
 4     public static int findnamestu(String[] names,int start,int end,String name) {
 5       int pos=-1;
 6       for(int i=start;i<=end;++i) {
 7         if(names[i].equals(name)) {
 8           pos=i;
 9         }
10       }
11       return pos;
12     }
13   public static void main(String[] args) {
14     String [] names= {"张三","李四","王二","麻子","拉大"};
15     int start =0;
16     int end=4;
17     String name="王二";
18     int result=findnamestu(names, start, end, name);
19     
20     System.out.println(result);
21   }
22 
23 }
24 
25 package 类的练习;
26 
27 public class Student {
28       String name;
29       int javaScore;
30       int sqlScore;
31       
32       public double getAvg() {
33         return getSum()/2.0;
34       }
35       
36       public int getSum() {
37         return javaScore+sqlScore;
38       }
39 }
40 
41 
42 package 类的练习;
43 //有三个学生,每个学生有姓名,两门成绩,计算每个学生的平均分,总分,总分按照从小到大排序
44 public class TestStudent {
45     public static void compareStudent(Student[] students) {
46       //冒泡排序
47       for(int i=0;i<students.length-1;++i) {
48         for(int j=0;j<students.length-1-i;++j) {
49           if(students[j].getSum()<students[j+1].getSum()) {
50             Student temp=students[j];
51             students[j]=students[j+1];
52             students[j+1]=temp;
53           }
54         }
55       }
56     }
57   public static void main(String[] args) {
58      Student stu1=new Student();
59      stu1.name="张三";
60          stu1.javaScore=47;
61          stu1.sqlScore=38;
62          
63          Student stu2=new Student();
64      stu2.name="李四";
65          stu2.javaScore=68;
66          stu2.sqlScore=97;
67          
68          Student stu3=new Student();
69      stu3.name="王二";
70          stu3.javaScore=89;
71          stu3.sqlScore=98;
72          
73          double stu1Avg=stu1.getAvg();
74          double stu2Avg=stu2.getAvg();
75          double stu3Avg=stu3.getAvg();
76          System.out.println(stu1.name+"\t总分为:"+stu1.getSum()+"\t平均分为:"+stu1.getAvg());
77          System.out.println(stu2.name+"\t总分为:"+stu2.getSum()+"\t平均分为:"+stu2.getAvg());
78          System.out.println(stu3.name+"\t总分为:"+stu3.getSum()+"\t平均分为:"+stu3.getAvg());
79          System.out.println("*************************************************");   
80          Student [] students=new Student[] {stu1,stu2,stu3};
81          
82          for(Student student: students) {
83            System.out.println(student.name+"\t总分为:"+student.getSum()+"\t平均分为:"+student.getAvg());
84          }
85          compareStudent(students);
86          
87          //排序后的数据
88          System.out.println("*************************************************");
89          for(Student student: students) {
90            System.out.println(student.name+"\t总分为:"+student.getSum()+"\t平均分为:"+student.getAvg());
91          }
92   }
93 
94 }

 

posted @ 2020-11-21 19:03  丁帅帅dss  阅读(144)  评论(0)    收藏  举报