教师需要计算不同年级、班级学生的平均分,因此需要在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;
}
}