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

huangapple go评论95阅读模式
英文:

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());
  }
  
}
英文:
  • name|Math|Reading|Science|
  • 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

得分: 1

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

从我看到的情况来看,在循环中,你可以找到姓名,并找到最高数学成绩。在那个循环中,你的 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;
}
英文:

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

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

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定