程序中for(int x:nums){ 这句是什么意思,它不是比较的意思嘛,怎么放在这里
publicclassCalculation{publicfloatavg(int...nums){intsum=0;for(intx:nums){sum+=x;}ret...
public class Calculation{
public float avg(int...nums){
int sum=0;
for(int x:nums){
sum + =x;
}
return((float)sum)/nums.length;
}
punlic static void main(String[]args){
Calculation cal=new Calculation();
float average1=cal.avg(10,20,30);
float average2=cal.avg(5,6,7,8,9,10);
System.out.println("The average of 10,20,30 is"+""+average1);
System.out.println("The average of 5,6,7,8,9,10 is"+""+average2);
}
} 展开
public float avg(int...nums){
int sum=0;
for(int x:nums){
sum + =x;
}
return((float)sum)/nums.length;
}
punlic static void main(String[]args){
Calculation cal=new Calculation();
float average1=cal.avg(10,20,30);
float average2=cal.avg(5,6,7,8,9,10);
System.out.println("The average of 10,20,30 is"+""+average1);
System.out.println("The average of 5,6,7,8,9,10 is"+""+average2);
}
} 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询