ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java 对象数组题目 + 改进(封装方法)

2021-09-12 10:34:15  阅读:131  来源: 互联网

标签:stus 封装 数组 int score Student 年级 Java


  1 /**
  2   * 
  3   * @Description 
  4   * @author  Bytezero·zhenglei!      Email:420498246@qq.com
  5   * @version
  6   * @date 2021年9月12日上午9:06:47
  7   * @
  8   *   对象数组题目
  9   * 定义类:Student,包含三个属性:学号number(int),年级state(int),
 10   *   成绩score(int)
 11   * 创建20个学生对象,学号为1-20,年级和成绩都有随机数确定
 12   * 问题一:打印出3年级(state值为3)的学生信息
 13   * 问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
 14   * 
 15   *  提示:生成随机数 Math.random(),返回值double
 16   *        四舍五入取整数:Math.round(double d),返回值类型long
 17   */
 18 public class StudentTest
 19 {
 20     public static void main(String[] args)
 21     {
 22         //声明Student类型的数组
 23         Student[] stus = new Student[20];
 24         for(int i =0; i <stus.length;i++)
 25         {
 26             //给数组元素赋值
 27             stus[i] = new Student();
 28             //给student对象的属性赋值
 29             stus[i].number= (i+1);
 30             //要求年级[1-6]
 31             stus[i].state =(int)(Math.random()*(6-1+1)+1);
 32             //成绩 [0-100]
 33             stus[i].score = (int)(Math.random()*(100-0+1)+0);
 34         }
 35         
 36         //遍历学生数组
 37         for(int i =0; i <stus.length; i++)
 38         {
 39             //System.out.println(stus[i].number+"\t"+stus[i].score+"\t"+stus[i].state);
 40                 
 41             System.out.println(stus[i].info());
 42         }
 43         
 44         System.out.println("******************************************");
 45         //问题一:打印出3年级(state值为3)的学生信息
 46         for(int i = 0; i <stus.length;i++)
 47         {
 48             if(stus[i].state==3)
 49             {
 50                 System.out.println(stus[i].info());
 51             }
 52         
 53         }
 54         
 55          //问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
 56         System.out.println("******************************************");
 57         System.out.println( "问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息");
 58     
 59        for(int i =0;i <stus.length-1;i++)
 60        {
 61            for(int j =0;j<stus.length-1-i;j++)
 62            {
 63                if(stus[j].score>stus[j+1].score)
 64                {
 65                    //交换的是数组的元素:Student对象!!!
 66                    Student temp = stus[j];
 67                    stus[j] = stus[j+1];
 68                    stus[j+1] = temp;
 69                }
 70            }
 71        }
 72     
 73      //遍历学生数组
 74     for(int i =0; i <stus.length; i++)
 75     {
 76         //System.out.println(stus[i].number+"\t"+stus[i].score+"\t"+stus[i].state);
 77             
 78         System.out.println(stus[i].info());
 79     }
 80     
 81     
 82     }
 83 }
 84 
 85 class Student
 86 {
 87     int number;   //学号
 88     int state;      //年级
 89     int score;    //成绩
 90     
 91     
 92     //显示学生信息的方法
 93     public String info()
 94     {
 95         return"学号:"+number+"\t年级:"+state+"\t成绩:"+score;
 96     }
 97     
 98 }
 99 
100 
101 
102 /****************************改进**************************/
103 
104  /**
105   * 
106   * @Description 
107   * @author  Bytezero·zhenglei!      Email:420498246@qq.com
108   * @version
109   * @date 2021年9月12日上午9:06:47
110   * @
111   *   对象数组题目
112   * 定义类:Student,包含三个属性:学号number(int),年级state(int),
113   *   成绩score(int)
114   * 创建20个学生对象,学号为1-20,年级和成绩都有随机数确定
115   * 问题一:打印出3年级(state值为3)的学生信息
116   * 问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
117   * 
118   *  提示:生成随机数 Math.random(),返回值double
119   *        四舍五入取整数:Math.round(double d),返回值类型long
120   */
121 
122 /***********    改进    **********/
123 /*将操作数组的功能封装到方法中*/
124 
125 
126 
127 public class StudentTest2
128 {
129     public static void main(String[] args)
130     {
131         //声明Student类型的数组
132         Student2[] stus = new Student2[20];
133         for(int i =0; i <stus.length;i++)
134         {
135             //给数组元素赋值
136             stus[i] = new Student2();
137             //给student对象的属性赋值
138             stus[i].number= (i+1);
139             //要求年级[1-6]
140             stus[i].state =(int)(Math.random()*(6-1+1)+1);
141             //成绩 [0-100]
142             stus[i].score = (int)(Math.random()*(100-0+1)+0);
143         }
144         
145         StudentTest2 test = new StudentTest2();
146         //遍历学生数组
147         test.print(stus);
148      
149         //问题一:打印出3年级(state值为3)的学生信息
150         System.out.println("******************************************");
151         System.out.println("问题一:打印出3年级(state值为3)的学生信息");
152          test.serchState(stus, 3);
153         
154         
155         //问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
156         System.out.println("******************************************");
157         System.out.println( "问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息");
158         test.sort(stus);
159         //遍历学生数组
160         test.print(stus);
161 
162     }
163     
164     /**
165      * 
166      * @Description 遍历Student2[]数组的操作
167      * @author  Bytezero·zhenglei!
168      * @date 2021年9月12日上午9:56:45
169      * @param stus
170      *
171      */
172     public void print(Student2[] stus)
173     {
174         //遍历学生数组
175         for(int i =0; i <stus.length; i++)
176         {
177             //System.out.println(stus[i].number+"\t"+stus[i].score+"\t"+stus[i].state);
178                 
179             System.out.println(stus[i].info());
180         }
181     }
182     
183     /**
184      * 
185      * @Description  查找Student数组中指定的学生信息
186      * @author  Bytezero·zhenglei!
187      * @date 2021年9月12日上午9:52:52
188      * @param stus  要查找的数组
189      * @param state  要找的年级
190      *
191      */
192   public void serchState(Student2[] stus,int state)
193   {
194     //问题一:打印出3年级(state值为3)的学生信息
195     for(int i = 0; i <stus.length;i++)
196     {
197         if(stus[i].state==state)
198         {
199             System.out.println(stus[i].info());
200         }
201     
202     }
203   }
204     
205   /**
206    * 
207    * @Description  给Student2数组排序  
208    * @author  Bytezero·zhenglei!
209    * @date 2021年9月12日上午9:55:35
210    * @param stus
211    *
212    */
213   public void sort(Student2[] stus)
214   {
215       for(int i =0;i <stus.length-1;i++)
216        {
217            for(int j =0;j<stus.length-1-i;j++)
218            {
219                if(stus[j].score>stus[j+1].score)
220                {
221                    //交换的是数组的元素:Student对象!!!
222                    Student2 temp = stus[j];
223                    stus[j] = stus[j+1];
224                    stus[j+1] = temp;
225                }
226            }
227        }
228   }
229   
230   
231     
232     
233 }
234 
235 class Student2
236 {
237     int number;   //学号
238     int state;      //年级
239     int score;    //成绩
240     
241     
242     //显示学生信息的方法
243     public String info()
244     {
245         return"学号:"+number+"\t年级:"+state+"\t成绩:"+score;
246     }
247     
248 }

 

标签:stus,封装,数组,int,score,Student,年级,Java
来源: https://www.cnblogs.com/Bytezero/p/15257493.html

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

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

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

ICode9版权所有