找到学生的最小值和平均值如果存在则显示学生的姓名

时间:2016-01-07 22:46:39

标签: java arrays

A- WO#
B- Priority
C- Equipment #
D- Description
E- Brief Description of Problem
F- Shutdown WO Y/N

} //我们在学校里有(年龄)和(姓名) - >发现“约翰”是学校里打印“他的年龄”的名字,如果有超过“一个约翰”的印刷品,所有年龄段的学生都称“约翰”,谁能帮我解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

尝试添加一个for循环来calcolate这样的年龄总和:

    for(i=0;i<age.length;i++){
        sum+=age[i];
      }

并在for循环中删除以下块:

  avg= sum/age.length;
  System.out.println("the avarage of all Students are :"+avg);
  System.out.println("the minimum age of all Students : "+min);

像这样:

  public static void main(String[] args) {
    // TODO code application logic here

   int[] age = new int []{21,20,19,18,18,};
   String name [] = {"sofia","maria","john","Petra","mark"};
  int sum = 0;
  int avg;
  int min=age[0];
  int i;
  int counter=0;
  for(i=0;i<age.length;i++){
  if(age[i]<min ) {
     min=age[i];

   }
 }
  for(i=0;i<age.length;i++){
        sum+=age[i];
      }

  avg= sum/age.length;
  System.out.println("the avarage of all Students are :"+avg);
  System.out.println("the minimum age of all Students : "+min);
  for(i=0;i<age.length;i++){
  if (age[i] == min ) {
          System.out.println("the minimum age of all Students  : "+name[i]); 
        }

     }
    }
   } 

输出:

 the avarage of all Students are :19
 the minimum age of all Students : 18
 the minimum age of all Students  : Petra
 the minimum age of all Students  : mark