求某位同学的平均分

12 篇文章 0 订阅

教师需要计算不同年级、班级学生的平均分,因此需要在Main类中,写一个方法,帮教师可以求一个同学的,所有课程成绩的平均分,方法首部为:

public int getAvg(String name, int... scores)
在main方法中使用该方法求两个学生的平均分,第一个学生有3门课程,第二个学生有4门课程,分别输入某位学生的姓名,以及各门课程的分值,调用getAvg方法求得平均分并显示。

输入格式:

每一行输入某个学生的姓名,各门课程的分值

输出格式:

每一行输出一个学生的姓名和平均分

输入样例:

在这里给出一组输入。例如:

tom 78 99 98
mary 88 99 98 97

输出样例:

在这里给出相应的输出。例如:

tom:91
mary:95

代码样例:

import java.util.Scanner;
public class Main{
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		String name1,name2;
		int score1,score2,score3;
		int score11,score21,score31,score4;
		name1 = in.next();
        score1 = in.nextInt();
        score2 = in.nextInt();
        score3 = in.nextInt();
        
        name2 = in.next();
        score11 = in.nextInt();
        score21 =in.nextInt();
        score31 = in.nextInt();
        score4 = in.nextInt();
        System.out.println(name1+":"+getAvg(name1,score1,score2,score3));
        System.out.println(name2+":"+getAvg(name2,score11,score21,score31,score4));

	}
		public static int getAvg(String name,int... scores){//求取平均分
	        int sum = 0;
	        for(int i : scores){
	            sum += i;
	        }
	        return sum/scores.length;
	    }

		
	}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值