简体   繁体   English

如何从多个数组中找到最大值

[英]How to find the highest value from multiple array

  • name|Math|Reading|Science|姓名|数学|阅读|科学|
  • Bob |75.0|57.0|65.0|鲍勃 |75.0|57.0|65.0|
  • Tom |60.0|67.0|75.0|汤姆 |60.0|67.0|75.0|
  • James|80.0|60.0|57.0|詹姆斯|80.0|60.0|57.0|
  • John|70.0|90.0|69.0|约翰|70.0|90.0|69.0|

From the above graph and below Java program, How do I find the person who gets highest Math score to change highest() method?从上面的图表和下面的 Java 程序,我如何找到数学分数最高的人来改变最高()方法?

Maybe, my highest() method gets highest Math score, but I don't know how to connect the highest score and the person.也许,我的最高() 方法获得了最高的数学分数,但我不知道如何将最高分和人联系起来。

public class Test {
  // Assuming that the first line(name, Math, Reading, Science) is skipped
  static int NAME = 4;
  static int SCORE = 3;
  static String[] name = new String[NAME]; 
  static Double[][] score = new Double[NAME][SCORE];

 public static String highest() { 
      double max = 0;
        for (int i = 0; i < NAME; i++) {
            if (max < score[i][0]) {
                max = score[i][0];
            }
        }
  }

  public static void main(String[] args) {
    System.out.println("The highest Math score is" + highest());
  }
  
}

EDIT: If the question is asking how to load data please tell me in the comments.编辑:如果问题是询问如何加载数据,请在评论中告诉我。 I assume you have loaded the data correctly.我假设您已正确加载数据。

As I see you can find the names in the for loop you find the max math score.正如我所见,您可以在 for 循环中找到名称,从而找到最大数学分数。 In that loop your i variable also can get the name like name[i] .在该循环中,您的i变量也可以获得类似name[i] The only thing you should do is you have to store the name of the student who has the highest score.您唯一应该做的就是存储得分最高的学生的姓名。 Just edit your function like below.只需编辑您的功能,如下所示。

public static String highest() { 
      String highestScoreStudent= "";
      double max = 0;
        for (int i = 0; i < NAME; i++) {
            if (max < score[i][0]) {
                max = score[i][0];
                highestScoreStudent = name[i];
            }
        }
      return highestScoreStudent;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM