Day_11【集合】扩展案例1_遍历打印学生信息,获取学生成绩的最高分,获取成绩最高的学员,获取学生成绩的平均值,获取不及格的学员数量
分析以下需求,并用代码实现:
1.按照以下描述完成类的定义
学生类
属性:
姓名name
年龄age
成绩score
行为:
吃饭eat()
study(String content)(content:表示学习的内容)
2.定义学生工具StudentsTool,有四个方法,描述如下
public void listStudents(Student[] arr):遍历打印学生信息
public int getMaxScore(Student[] arr):获取学生成绩的最高分
public Student getMaxStudent(Student[] arr):获取成绩最高的学员
public int getAverageScore(Student[] arr):获取学生成绩的平均值
public int getCount(Student[] arr):获取不及格的学员数量 3.定义测试类TestStudentTool
在main方法中首先创建长度为5的Student数组并初始化数据
再创建StudentsTool类的对象,并调用以上方法
package com.itheima;
public class Student {
	//定义学生类属性
	private String name;
	private int age;
	private int score;
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(String name, int age, int score) {
		super();
		this.name = name;
		this.age = age;
		this.score = score;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	public static void eat() {}
	public static void study(String content) {}
}
package com.itheima;
public class StudentTool {
	// public void listStudents(Student[] arr):遍历打印学生信息
	public void listStudents(Student[] arr) {
		for (int x = 0; x < arr.length; x++) {
			Student s = arr[x];
			System.out.println(s.getName() + "---" + s.getAge() + "---" + s.getScore());
		}
	}
	// public Student getMaxStudent(Student[] arr):获取成绩最高的学员
	public Student getMaxStudent(Student[] arr) {
		Student s = arr[0];
		for (int x = 1; x < arr.length; x++) {
			if (arr[x].getScore() > s.getScore()) {
				s = arr[x];
			}
		}
		return s;
	}
	// public int getMaxScore(Student[] arr):获取学生成绩的最高分
	public int getMaxScore(Student[] arr) {
		Student s = getMaxStudent(arr);
		return s.getScore();
	}
	// public int getAverageScore(Student[] arr):获取学生成绩的平均值
	public int getAverageScore(Student[] arr) {
		// 先定义一个总值变量
		int sum = 0;
		for (int x = 0; x < arr.length; x++) {
			Student s = arr[x];
			sum += s.getScore();
		}
		return sum / arr.length;
	}
	//	public int getCount(Student[] arr):获取不及格的学员数量
	public int getCount(Student[] arr){
		int count = 0;
		for(int x = 0;x < arr.length;x++) {
			Student s = arr[x];
			if(s.getScore() < 60) {
				count++;
			}
		}
		return count;
	}
}
package com.itheima;
public class TestStudentTool {
	public static void main(String[] args) {
		//创建Student对象
		Student s1 = new Student("马化腾",19,70);
		Student s2 = new Student("求伯君",21,90);
		Student s3 = new Student("雷军",20,80);
		Student s4 = new Student("马云",19,40);
		Student s5 = new Student("刘强东",19,30);
		//创建长度为5的Student数组
		Student[] arr = {s1,s2,s3,s4,s5};
		//创建StudentTool的对象
		StudentTool st = new StudentTool();
		//遍历打印学生信息
		st.listStudents(arr);
		//获取学生成绩的最高分
		System.out.println("几位学生中成绩最高分为:"+st.getMaxScore(arr));
		//获取成绩最高的学生姓名
		Student s = st.getMaxStudent(arr);
		System.out.println("成绩最高的学生为:"+s.getName());
		//获取学生成绩的平均值
		System.out.println("学生的平均成绩为:"+st.getAverageScore(arr));
		//获取不及格的学员数量
		System.out.println("不及格的学生数量为:"+st.getCount(arr));
	}
}
控制台输出内容

Day_11【集合】扩展案例1_遍历打印学生信息,获取学生成绩的最高分,获取成绩最高的学员,获取学生成绩的平均值,获取不及格的学员数量的更多相关文章
- java开发学生信息管理系统的实现(简洁易懂),适合计算机专业学生参考,课程设计、毕业论文设计参考等
		
编写一个简单的学生管理信息系统. 在oracle中设计一张学生表,以学号作为关键字. 其他学生信息有:姓名.手机号. 在进入系统时,显示如下菜单: ************************** ...
 - Day_12【集合】扩展案例1_利用集合的知识对长度为10的int数组进行去重,产生新数组,不能改变数组中原来数字的大小顺序
		
分析以下需求,并用代码实现 1.定义一个长度为10的int数组,并存入10个int类型的数据,其中有一些数据是重复的 2.利用集合的知识对数组进行去重,产生新数组,不能改变数组中原来数字的大小顺序 3 ...
 - Day_14【IO流】扩展案例1_缓冲区字符输入、输出流,键盘录入,contains()方法的统一运用
		
分析以下需求,并用代码实现 实现一个验证码小程序,要求如下: 1. 在项目根目录下新建一个文件:data.txt,键盘录入3个字符串验证码,并存入data.txt中,要求一个验证码占一行: 2. 键盘 ...
 - Day_13【IO流】扩展案例1_读取项目文件内容并去重
		
分析以下需求,并用代码实现: 需求: 读取当前项目下的info1.txt 文件内容如下 : aaaaaaaaaaabbbbbbbbbbbbccdefg 要求将数据去重后写回最终效果 : fgdebca ...
 - Day_09【常用API】扩展案例1_程序中使用一个长度为3的对象数组,存储用户的登录名和密码……
		
需求说明:实现用户注册.登陆功能: 1.程序中使用一个长度为3的**对象数组**,存储用户的登录名和密码: 例如如下格式: 登录名 密码 生日 爱好 zhangsan 1111 1998-03-15 ...
 - Day_08【面向对象】扩展案例1_测试项目经理类和程序员类
		
分析以下需求,并用代码实现: 1.定义项目经理类 属性: 姓名 工号 工资 奖金 行为: 工作work 2.定义程序员类 属性: 姓名 工号 工资 行为: 工作work 要求: 向上抽取一个父类,让这 ...
 - Day_10【常用API】扩展案例1_利用人出生日期到当前日期所经过的毫秒值计算出这个人活了多少天
		
分析以下需求,并用代码实现: 1.从键盘录入一个日期字符串,格式为 xxxx-xx-xx,代表该人的出生日期 2.利用人出生日期到当前日期所经过的毫秒值计算出这个人活了多少天 package com. ...
 - 面向对象案例 - 学生信息管理系统V1.0
		
学生管理系统项目[所有知识点整合] 1. 学生管理系统项目 尝试完成以下功能 实体类: 学生类: id, 姓名,年龄,性别,成绩 需要使用数组保存学生信息 Student[] allStu 需要完成的 ...
 - 利用Java集合实现学生信息的”增删查“
		
之前学了Java中的集合,打算写一个小程序来消化一下! 那么我们知道,集合相比数组的优点就是可以动态的增加元素,这对比数组来说,十分的便捷: 并且集合为我们封装好一些方法,可以更好的做一些数据操作! ...
 
随机推荐
- stand up meeting 1-6
			
今日更新: 1.修复初始最佳战绩显示bug: 初始为击败全国0% 用户 2.挑战结果界面显示“哎,今天的饭又白吃了,回去多吃两碗###”, 去除API返回string中的“###”. 3.分享模块初 ...
 - vue2.x学习笔记(二十三)
			
接着前面的内容:https://www.cnblogs.com/yanggb/p/12639440.html. 渲染函数&JSX 基础 vue推荐在绝大多数的情况下使用模板来创建html.然而 ...
 - Linux-LAMP虚拟主机配置
			
1.配置用户认证 <Directory /data/discuz/passwd> AllowOverride AuthConfig AuthName "自定义的" Au ...
 - Python logging 模块打印异常 exception
			
logger.exception(sys.exc_info())
 - 计算2的n次幂htm代码
			
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
 - 解决w3wp.exe占用CPU和内存问题
			
在WINDOWS2003+IIS6下,经常出现w3wp的内存占用不能及时释放,从而导致服务器响应速度很慢.可以做以下配置进行改善:1.在IIS中对每个网站进行单独的应用程序池配置.即互相之间不影响.2 ...
 - spring boot 集成mybatis使用logback打印并保存日志信息
			
spring boot 打印执行的sql语句 最近在学习spring boot 整合了Mybatis和druid之后总感觉少点什么东西,看了下在别的项目上用的框架,发现自己整合的东西不打印sql语句, ...
 - html入门详细笔记
			
Web的基本概念 什么是Web? 中文翻译"网页",它是一些列技术的总称,(包括网站的前台布局.后台程序.美工.数据库开发等),我们称它为网页. Web标准 结构标准(HTML) ...
 - 一个简单的wed服务器SHTTPD(7)———— SHTTPD内容类型的实现
			
//start from the very beginning,and to create greatness //@author: Chuangwei Lin //@E-mail:979951191 ...
 - 在linux上搭建nacos集群(步骤详细,linux小白也搞得定)
			
(1)nacos官网:https://github.com/alibaba/nacos/releases/tag/1.2.1下载nacos安装包到window本地(后缀为tar.zip) (2)在li ...