有5位学员参加了JAVA知识竞赛的决赛,利用方法计算决赛的平均成绩和最高成绩

package com.leiclass.cl;

import java.util.Scanner;

public class Test1 {

/**
* 计算五名学生平均分和最高分,
* 定义一个学生类数组,长度为5,
* 然后给数组的每一个元素创建一个对象,再将输入的分数赋值给类里面的成员变量
* 定义一个计算方法类,构造计算平均分的方法和最高分的方法


* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);

System.out.println("请输入五名参赛者的成绩:");
//创建一个对象数组
Student[] stu=new Student[5];//只能存放Student类的数据类型
Student stu1=new Student();//创建5个对象
Student stu2=new Student();
Student stu3=new Student();
Student stu4=new Student();
Student stu5=new Student();
stu[0]=stu1;//给对象初始化
stu[1]=stu2;
stu[2]=stu3;
stu[3]=stu4;
stu[4]=stu5;
for (int i = 0; i < stu.length; i++) {//循环输入五名学生成绩
stu[i].score=sc.nextInt();
}
Manager ma=new Manager();
double avg=ma.avg(stu);
int max=ma.maxScore(stu);
System.out.println("平均分为:"+avg+"\t最高分为:"+max);
}

}

package com.leiclass.cl;
/**
* 定义一个学生类
* @author THINK
*
*/
public class Student {
public int score;

}


package com.leiclass.cl;

public class Manager {
public double avg=0;
public int sum=0;
public int max=0;
public double avg(Student[] stu){
for (int i = 0; i < stu.length; i++) {
sum+=stu[i].score ;
}
return avg=sum/stu.length;
}
public int maxScore(Student[] stu){
for (int i = 0; i < stu.length; i++) {
if(stu[i].score>max){
max=stu[i].score;
}
}
return max;
}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值