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

go评论94阅读模式

How to find the highest value from multiple array

# 问题

``````public class Test {
// 假设第一行（姓名，数学，阅读，科学）被跳过
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("最高数学分数是" + highest());
}

}
``````

• Bob |75.0|57.0|65.0|
• Tom |60.0|67.0|75.0|
• James|80.0|60.0|57.0|
• John|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?

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 &lt; NAME; i++) {
if (max &lt; score[i][0]) {
max = score[i][0];
}
}
}

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

}
``````

# 答案1

EDIT: 如果问题是关于如何加载数据，请在评论中告诉我。我假设你已经正确加载了数据。

``````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;
}
``````

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. In that loop your `i` variable also can get the name like `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= &quot;&quot;;
double max = 0;
for (int i = 0; i &lt; NAME; i++) {
if (max &lt; score[i][0]) {
max = score[i][0];
highestScoreStudent = name[i];
}
}
return highestScoreStudent;
}
``````

• 本文由 发表于 2020年10月3日 18:50:41
• 转载请务必保留本文链接：https://go.coder-hub.com/64183396.html
• java

go 91

go 68

go 67

go 60