英文:
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 < 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());
}
}
答案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= "";
double max = 0;
for (int i = 0; i < NAME; i++) {
if (max < score[i][0]) {
max = score[i][0];
highestScoreStudent = name[i];
}
}
return highestScoreStudent;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论