【问题标题】:find the min and avg of student if it exist show the name of the student找到学生的最小值和平均值(如果存在)显示学生的姓名
【发布时间】:2016-01-07 22:46:38
【问题描述】:
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];

  }




  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]); 
        }

  }
}

} // 我们有学校的 (age) 和 (names) --> 找到 "john" 是学校的名字,如果有多个 "one john" 打印学生的所有年龄,则打印 "his age"打电话给“约翰”有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    尝试添加一个 for 循环来计算年龄总和,如下所示:

        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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多