![](/img/trans.png)
[英]How to find the position of the highest and second highest value in an array in java
[英]How to find the highest value from multiple array
從上面的圖表和下面的 Java 程序,我如何找到數學分數最高的人來改變最高()方法?
也許,我的最高() 方法獲得了最高的數學分數,但我不知道如何將最高分和人聯系起來。
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());
}
}
編輯:如果問題是詢問如何加載數據,請在評論中告訴我。 我假設您已正確加載數據。
正如我所見,您可以在 for 循環中找到名稱,從而找到最大數學分數。 在該循環中,您的i
變量也可以獲得類似name[i]
。 您唯一應該做的就是存儲得分最高的學生的姓名。 只需編輯您的功能,如下所示。
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.