qq_不唱流行歌曲的流天域_0
2016-12-10 10:08
package com.imooc;
public class Array {
public static void main(String[]args){
int []score=new int []{61,23,4,74,13,148,20};
double plus=0;
double ave=0;
int max=score[0];
for(int i=0;i<=score.length;i++)
{if(score[i]>max)
max=score[i];
else
max=score[0];
}
System.out.println("数组中的最大值"+max);
int min=score[0];
for(int i=0;i<=score.length;i++)
{if(score[i]<min)
min=score[i];
else
min=score[0];
}
System.out.println("数组中的最小值"+min);
for(int i=0;i<=score.length;i++)
{
plus+=score[i];
}
ave=plus/4;
System.out.println("数组中的平均值"+ave);
}
}
int []score=new int []{61,23,4,74,13,148,20};//定义了数组的长度为7
for(int i=0;i<=score.length;i++) //i <= score.length i就可以取值7 就超出了数组的长度
综上 把<=改成<就行了
package com.baidu.zx;
public class HomeTest {
public static void main(String[]args){
int []score=new int []{61,23,4,74,13,148,20};
double plus=0;
double ave=0;
int max=score[0];
int min=score[0];
for(int i=0;i<score.length;i++){
if(score[i]>max)
max=score[i];
}
System.out.println("数组中的最大值"+max);
for(int i=0;i<score.length;i++){
if(score[i]<min)
min=score[i];
}
System.out.println("数组中的最小值"+min);
for(int i=0;i<score.length;i++){
plus+=score[i];
}
ave=plus / (score.length);
System.out.println("数组中的总和为"+plus);
System.out.println("数组中的平均值"+ave);
}
}Java入门第一季(IDEA工具)
1168416 学习 · 18754 问题
相似问题