如何打印1个数组的最小分数和来自与其他数组对应的不同数组的名称

时间:2014-03-24 17:45:09

标签: java arrays

我需要打印每个数组的最小和最大数字以及与该数字对应的不同数组的学生姓名。

这是我到目前为止所做的。

import java.util。*;    import java.util.Arrays;

public class Lab5A2 {
public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
   meals(console);

}

public static void meals(Scanner console) {
    System.out.print("How many grades ");
    int people = console.nextInt();
    int subtotal = 0;
    int min = 10000;
    int max = 0;
    int avg = 0;
    int[] grades = new int[people];
    for (int i = 1; i <= people; i++) {
        System.out.print("Next grade? ");
        int personCost = console.nextInt();
        subtotal = subtotal + personCost;
        grades[i-1] = personCost;
        if(max <personCost)
        {
          max = personCost;
        }
        if(min>personCost)
          min = personCost;
    }
  String[] names = new String[people];
 System.out.println("Now the names.");
 for (int j = 1; j <= people; j++) {
        System.out.print("Next name? ");
        String name = console.next();

        names[j-1] = name;

  }

    avg = subtotal/people;

    int Min = min;

    int Max = max;
    int Avg = avg;
    int Sum = subtotal;
    System.out.println(Min);
    System.out.println(min);
    System.out.println(Arrays.toString(grades));
    System.out.println(Arrays.toString(names));

     int hue = Arrays.binarySearch(grades, Min);
     int huey = Arrays.binarySearch(grades, Max);
  System.out.println(hue);
  System.out.println(huey);

    System.out.println("/*** Statistics from student data ***/");
    System.out.println("Max. Grade = " + grades[huey] + " " + "(" + names[huey] + ")");
    System.out.println("Min. Grade = " + grades[hue] + " " + "(" + names[hue] + ")");
    System.out.println("Avg. Grade = " + Avg);
    System.out.println(Arrays.toString(grades));
    System.out.println(Arrays.toString(names));
}
}

我知道我需要使用索引,但我不知道如何

0 个答案:

没有答案