zoukankan      html  css  js  c++  java
  • 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));
    	}
    
    }
    
    

    控制台输出内容
    控制台输出内容

  • 相关阅读:
    Advanced Configuration Tricks
    Reviewing the Blog Module
    Editing and Deleting Data
    Making Use of Forms and Fieldsets
    Understanding the Router
    SQL Abstraction and Object Hydration
    Preparing for Different Databases
    Java学习理解路线图
    Openstack学习历程_1_视频
    CentOS安装Nginx负载
  • 原文地址:https://www.cnblogs.com/zzzsw0412/p/12772531.html
Copyright © 2011-2022 走看看