java利用数组求平均值,最大值,最小值
程序员文章站
2023-12-19 10:13:10
复制代码 代码如下:(测试类) package com.badu; public class tste { &...
复制代码 代码如下:
(测试类)
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) {
}
}