ICode9

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

面向对象的学习代码,涉及求任意值数(Math.random()),声明Student类型的数组给数组元素赋值,给Student对象的属性赋值,利用冒泡排序法进行排序,将操作数组的功能封装到方法中

2021-05-13 19:01:21  阅读:141  来源: 互联网

标签:数组 int Student1 state Student test 年级 赋值


这个代码是面向对象的学习代码,

涉及求任意值数(Math.random()),

声明Student类型的数组给数组元素赋值,

给Student对象的属性赋值,利用冒泡排序法进行排序,

将操作数组的功能封装到方法中

package com.atlibrary.exer;
/*
 * 4. 对象数组题目:
定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。
 创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
问题一:打印出3年级(state值为3)的学生信息。
问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息

提示:
1) 生成随机数:Math.random(),返回值类型double;  
2) 四舍五入取整:Math.round(double d),返回值类型long。
 * 
 * 
 * 此代码是对StudentTest.java的改进:将操作数组的功能封装到方法中。
 * 
 */
public class StudentTest1 {
	public static void main(String[] args) {
		Student1[] test = new Student1[20];//给数组元素赋值   String[] name = new String[20];
		
		for(int i = 0;i < test.length;i++){ 
			test[i] = new Student1();//Person p1 = new Person(); 
			//对Student对象的属性赋值
			test[i].number = (i + 1);
			test[i].state =  (int)(Math.random() * ( 6 - 1 + 1 ) + 1);
			test[i].score = (int)(Math.random()*( 100 + 1 ));
		}
		//遍历数组
		StudentTest1 test1 = new StudentTest1();
		test1.print(test);

 		System.out.println("以下为年级为3的所有学生信息:");
		//问题一:打印出年级为3的所有学生信息
 		test1.searchState(test, 3);
 		System.out.println("**********");
		//问题二:使用冒泡排序对学生的成绩进行排序,输出排序过后的学生信息
		test1.sort(test);
		//遍历所有数组
		test1.print(test);
	}
	
	/**
	 * 
	*	@Description  封装遍历数组的方法
	*	@author Clark 
	*	@date 2021年4月19日下午2:47:21 
	*	@param test
	 */
	
	public void print(Student1[] test){
		for(int i = 0;i < test.length;i++){
			System.out.println(test[i].info());
		}
	}
	/**
	 * 
	*	@Description  查找Stduent数组中指定年级的学生信息
	*	@author Clark 
	*	@date 2021年4月19日下午2:47:43 
	*	@param test  要查找的数组
	*	@param state  要查找的年级
	 */
	//查找指定年级的学生的所有信息
	public void searchState(Student1[] test,int state){
		for(int i = 0;i < test.length;i++){
			if(test[i].state == state){
				System.out.println(test[i].info());
			}
		}
	}
	/**
	 * 
	*	@Description  使用冒泡排序对数组进行排序
	*	@author Clark 
	*	@date 2021年4月19日下午2:48:38 
	*	@param test
	 */
	//使用冒泡排序对数组进行排序
	public void sort(Student1[] test){
		for(int i = 0;i < test.length - 1;i++){
			for(int j = 0;j <test.length - 1 - i;j++ ){
				if(test[j].score > test[j + 1].score){
					Student1 temp = new Student1(); 
					temp = test[j];
					test[j] = test[j + 1];
					test[j + 1] = temp;
				}
			}
		}
	}
	
}
class Student1{
	//属性
	int number;
	int score;
	int state;
	//方法
	public String info(){
		return "学号:"+number+" 年级:"+state+" 分数:"+score;
	}
}

 

标签:数组,int,Student1,state,Student,test,年级,赋值
来源: https://blog.csdn.net/handsome1_/article/details/116761070

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

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

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

ICode9版权所有