主方法,键入五个学生的成绩
public static void main(String[] args) {
System.out.print("请输入五个学生的成绩;");
Scanner stu = new Scanner(System.in);
double soc1 = stu.nextDouble();
double soc2 = stu.nextDouble();
double soc3 = stu.nextDouble();
double soc4 = stu.nextDouble();
double soc5 = stu.nextDouble();
stu.close();
Double sunSco=sum(soc1,soc2,soc3,soc4,soc5);
Double avgSco=Avg(soc1,soc2,soc3,soc4,soc5);
System.out.println("五个学生成绩和为"+sunSco);
System.out.println("五个学生平均成绩为"+avgSco);
}
创建求五个学生成绩和的方法
public static double sum(double s1,double s2,double s3,double s4,double s5){
Double sum=s1+s2+s3+s4+s5;
return sum;
}
创建求五个学生平均成绩的方法
public static double Avg(double s1,double s2,double s3,double s4,double s5){
Double avg=sum(s1,s2,s3,s4,s5)/5;
return avg;
}
总代码
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
import sun.security.mscapi.SunMSCAPI;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.print("请输入五个学生的成绩;");
Scanner stu = new Scanner(System.in);
double soc1 = stu.nextDouble();
double soc2 = stu.nextDouble();
double soc3 = stu.nextDouble();
double soc4 = stu.nextDouble();
double soc5 = stu.nextDouble();
stu.close();
Double sunSco=sum(soc1,soc2,soc3,soc4,soc5);
Double avgSco=Avg(soc1,soc2,soc3,soc4,soc5);
System.out.println("五个学生成绩和为"+sunSco);
System.out.println("五个学生平均成绩为"+avgSco);
}
public static double sum(double s1,double s2,double s3,double s4,double s5){
Double sum=s1+s2+s3+s4+s5;
return sum;
}
public static double Avg(double s1,double s2,double s3,double s4,double s5){
Double avg=sum(s1,s2,s3,s4,s5)/5;
return avg;
}
}