簡體   English   中英

如何從多個數組中找到最大值

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

  • 姓名|數學|閱讀|科學|
  • 鮑勃 |75.0|57.0|65.0|
  • 湯姆 |60.0|67.0|75.0|
  • 詹姆斯|80.0|60.0|57.0|
  • 約翰|70.0|90.0|69.0|

從上面的圖表和下面的 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM