java利用数组求平均值,最大值,最小值
(测试类)
package com.badu;
public class Tste {
public static void main(String[] args) {
Class5 sa=new Class5("周阿郎",3,2,24);
Class5 [] a=new Class5[5];
a[0]=new Class5("张扬",2,4,34);
a [1]=new Class5("刘禅",3,2,24);
a[2]=new Class5("张燕",4,23,78);
a [3]=new Class5("周少博",32,23,99);
a [4]=new Class5("张旭",24,13,100);
System.out.println(sa.toString());
double [] w={a[0].getSJava(),a[1].getSJava(),a[2].getSJava(),a[3].getSJava(),a[4].getSJava()};
int s=0;
double max=w[2];
double min=w[0];
for(int i=0;i<5;i++){
s+=w[i]/5;
if(max<w[0]){
max=w[2];
}
if(min>w[0]){
min=w[0];
}
}
System.out.println("java成绩的平均值为"+s+",最小值为"+min+",最大值为"+max);
}
}
(一)
package com.badu;
public class Class5 {
private String name;
private int age;
private int sno;
private int sJava;
public Class5(String a,int w,int g,int l){
name=a;
age=w;
sno=g;
sJava=l;
}public Class5(int b){
age=b;
}public Class5(int c,int d){
sno=c;
sJava=d;
}
public Class5(){}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public int getNo(){
return sno;
}
public int getSJava(){
return sJava;
}
public String toString (){
String s="我叫"+name+",我今年"+age+"岁"+"我的java成绩是"+sJava;
return s;
}
public static void main(String[] args) {
}
}