ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

面向对象_上

2022-03-20 23:04:03  阅读:164  来源: 互联网

标签:stus 20 int 面向对象 state score Student


1/定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。 创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。

  问题一:打印出3年级(state值为3)的学生信息。

  问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息

 1 public class StudentsTest {
 2     public static void main(String[] args) {
 3         //第二大部  创建类的对象
 4         
 5         //创建20个学生对象,太多 使用数组
 6         //Student s1 = new Student();
 7         //Student s2 = new Student();
 8         //......................
 9         
10         //使用数组  创建20个元素,来容纳20个student
11         Student[] stus = new Student[20];//使用数组对象   声明数组和使用动态初始化,说明有20个student
12         for(int i = 0;i < stus.length;i++)//使用for循环,遍历整个数组元素
13         {
14             //创建对象,stus[i]由元素变成类的对象
15             stus[i] = new Student();//等同于Student s1 = new Student();(创建对象)  stus[i] 等同于 s1
16             
17             
18             //第三大部: 调用属性
19             stus[i].number = i + 1;//编排学号 1-20
20             stus[i].state = (int)(Math.random()*(6 - 1 + 1) + 1);//定义年级为[1,6]  此时采用的是随机数截断   Math.random()*(b - a + 1) + a                 
21             stus[i].score = (int)(Math.random()*(100 - 0 + 1) + 0);//定义成绩为[0,100]  此时采用的是随机数截断 
22         }
23         //遍历学生信息
24         for(int i = 0;i < stus.length;i++)
25         {
26             System.out.println(stus[i].info());
27         }
28         System.out.println("******************************");
29         //打印出3年级(state值为3)的学生信息。
30         for(int i = 0;i < stus.length;i++)
31         {
32             if(stus[i].state == 3)
33             {
34                 System.out.println(stus[i].info());
35             }
36         }
37         //使用冒泡排序按学生成绩排序,并遍历所有学生信息,排序角标都是从0开始标注第一个元素
38         for(int i = 0;i < stus.length - 1;i++)//若数组有7个,只需要比较6次,也就是需要遍历6轮,即角标为0-5
39         {
40             for(int j = 0;j < stus.length - 1 - i;j++)//例如遍历第0轮,需要比较6个元素,即角标为0-5,遍历1轮,需要比较5个元素,即角标为0-4
41             {
42                 if(stus[j].score >= stus[j + 1].score)//比较成绩
43                 {
44                     //此时交换学生成绩排名,不是交换成绩本身
45                     Student temp = stus[j];//temp:临时
46                     stus[j] = stus[j + 1];
47                     stus[j + 1] =  temp;
48                 }
49             }
50         }
51         System.out.println("******************************");
52         
53         //遍历所有学生信息
54         for(int i = 0;i < stus.length;i++)
55         {
56             System.out.println(stus[i].info());
57         }
58         
59     }
60 }
61 //第一大部:创建类,设计类的成员
62 class Student
63 {
64     //设计属性
65     int number;
66     int state;
67     int score;
68     //设计方法
69     public String info()
70     {
71         return("学号:" + number + "\t年级:" + state + "\t成绩:" + score);
72     }
73 }

 

2/对上一题的优化

 1 public class StudentTest2 
 2 {
 3     public static void main(String[] args) 
 4     {
 5         //第二大部  创建Student2类的对象
 6     
 7         //使用数组  创建20个元素,来容纳20个student
 8         Student2[] stus = new Student2[20];
 9         for(int i = 0;i < stus.length;i++)//使用for循环,遍历整个数组元素
10         {
11             //创建对象,stus[i]由元素变成类的对象
12             stus[i] = new Student2();
13             //第三大部: 调用属性
14             stus[i].number = i + 1;//编排学号 1-20
15             stus[i].state = (int)(Math.random()*(6 - 1 + 1) + 1);//定义年级为[1,6]  此时采用的是随机数截断   Math.random()*(b - a + 1) + a                 
16             stus[i].score = (int)(Math.random()*(100 - 0 + 1) + 0);//定义成绩为[0,100]  此时采用的是随机数截断 
17         }
18         
19         //创建StudentTest2类的对象
20             StudentTest2 test = new StudentTest2();
21             //遍历学生信息
22             test.print(stus);
23             System.out.println("*********************");
24             //state == 3
25             test.seacherState(stus, 3);
26             System.out.println("*********************");
27             //冒泡排序 并打印学生信息
28             test.sort(stus);
29             test.print(stus);
30     }
31     //设计方法,将功能封装到方法中
32     //注意不能写在上一个public里面
33     //遍历学生信息
34     public void print(Student2[] stus)
35     {
36         for(int i = 0;i < stus.length;i++)
37         {
38             System.out.println(stus[i].info());
39         }
40     }
41     //查找Student数组中指定年级的学生信息
42     public void seacherState(Student2[] stus,int state)
43     {
44         for(int i = 0;i < stus.length;i++)
45         {
46             if(stus[i].state == state)
47             {
48                 System.out.println(stus[i].info());
49             }
50         }
51     }
52     //排序成绩
53     public void sort(Student2[] stus)
54     {
55         for(int i = 0;i < stus.length - 1;i++)
56         {
57             for(int j = 0;j < stus.length - 1 - i;j++)
58             {
59                 if(stus[j].score >= stus[j + 1].score)
60                 {
61                     Student2 temp = stus[j];
62                     stus[j] = stus[j + 1];
63                     stus[j + 1] = temp;
64                 }
65             }
66         }
67     }
68 }
69 //第一大部:创建类,设计类的成员
70 class Student2
71 {
72     //设计属性
73     int number;
74     int state;
75     int score;
76 //设计方法
77     public String info()
78     {
79         return("学号:" + number + "\t年级:" + state + "\t成绩:" + score);
80     }
81 }

 

标签:stus,20,int,面向对象,state,score,Student
来源: https://www.cnblogs.com/truth847453497/p/16032587.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有