ICode9

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

简单的面向对象编程

2021-03-12 11:02:38  阅读:158  来源: 互联网

标签:int Student2 state score 面向对象编程 简单 test public


package com.atanjin.exercise;

/*此代码是练习1的一个改进,将操作数组的功能封装到方法中
 * 
 */

public class MethodExercise2 {
	public static void main(String[] args) {
		Student2 s[] = new Student2[20];
		for(int i = 0;i < s.length;i++) {
			s[i] = new Student2();
			s[i].score = (int)(Math.random()*100 + 1);
			s[i].state = (int)(Math.round(Math.random() * 6 + 0.5));
			s[i].number = i + 1;
		}
		MethodExercise2 test = new MethodExercise2();
		test.print(s);
		System.out.println("***********************");
		test.searchStste(s, 3);
		System.out.println("***********************");
		test.sort(s);
		test.print(s);
	}
	//遍历Studengt2[]的操作
	/**
	 * 
	 * @Descrition : 遍历数组
	 * @author AnJin
	 * @date 2021年3月12日上午10:48:56
	 * @param s
	 */
	public void print(Student2 s[]) {
		for(int i = 0;i < s.length;i++) {
			System.out.println(s[i].info());
		}
	}
	/**
	 * 
	 * @Descrition : 查找指定年级的学生
	 * @author AnJin
	 * @date 2021年3月12日上午10:45:27
	 * @param s:要查找的数组
	 * @param state:要查找的年级
	 */
	public void searchStste(Student2 s[],int state) {
		for(int i = 0;i < s.length;i++) {
			if(s[i].state == state) {
				System.out.println(s[i].info());
			}
		}
	}
	/**
	 * 
	 * @Descrition:给数组排序
	 * @author AnJin
	 * @date 2021年3月12日上午10:48:21
	 * @param s
	 */
	public void sort(Student2 s[]) {
		for(int i = 0;i < s.length - 1;i++) {
			for(int j = 0;j < s.length - i - 1;j++) {
				if(s[j].score < s[j + 1].score) {
					Student2 temp = s[j];
					s[j]= s[j + 1];
					s[j + 1]= temp; 
				}
			}
		}
	}
}
class Student2 {
	int number;
	int state;
	int score;
	public String info() {
		String info = "学号:" + number + "\t年级:" + 
				state + "\t成绩:" + score;
		return info;
	}
}

  

标签:int,Student2,state,score,面向对象编程,简单,test,public
来源: https://www.cnblogs.com/aj-0121/p/14522579.html

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

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

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

ICode9版权所有