如何找到数组中的最大数字

时间:2022-08-25 19:31:24

I have an array of grades(g) which is an int[] and I'm trying to find the largest grade in that array. I have tried this:

我有一个等级(g)的数组,这是一个int [],我试图找到该数组中最大的等级。我试过这个:

public static String highestGradeName(int[] g, String[] n) {
    String highStudent;
    int highest = g[0];
    for (int i=1; i < g.length; i++) {
        if (highest < g[i]) {
            highStudent = n[i+1];
            return (highStudent);
        }
    }
    return null;
}

I have another array which is a String array and contains the names of the students, I have the return null there because it said it needed a return statement however I didn't plan on it ever being null. What's causing it to return null instead of highstudent?
I've used the exact code to find the lowest grade and it works fine the only thing I did to this one was change the if statement from highest > g[i] to highest < g[i].

我有另一个数组是一个字符串数组,并包含学生的名字,我有返回null因为它说它需要一个return语句,但我没有计划它永远是null。是什么导致它返回null而不是highstudent?我已经使用了确切的代码来找到最低等级,并且它工作得很好,我对此做的唯一一件事是将if语句从最高> g [i]更改为最高

2 个解决方案

#1


3  

Returning from inside the loop is wrong, as you can always have an even larger number later on in the array. You should keep the index of the highest grade and just return the corresponding name at the end:

从循环内部返回是错误的,因为稍后在数组中总是可以有更大的数字。您应该保留最高等级的索引,并在最后返回相应的名称:

public static String highestGradeName(int[] g, String[] n) {
    int highest = 0;
    for (int i = 1; i < g.length; i++) {
        if (g[highest] < g[i]) {
            highest = i;
        }
    }
    return n[highest];
}

#2


0  

According to your logic let's break things.

根据你的逻辑,让我们打破局面。

Raw Test Case:

原始测试案例:

Take {0,1,2} as an integer arrays. Take {"arya", "jon", "tyrion"} as an string arrays.

将{0,1,2}作为整数数组。以{“arya”,“jon”,“tyrion”}作为字符串数组。

highest = 0; // according to your code.

For int i = 1, i < 3; i++
0 < 1
highstudent = 2 // tyrion
// returns tyrion

The reason why you are getting null is your integer at 0 index should be greater or equal to the index at 1.

你得到null的原因是你的0整数索引应该大于或等于1的索引。

Now, when you are using a type String in your method. You should return a string and that's what your editor said to have something return. You should use return out of the loop because you need to find the highest student which is only possible after looping all the list.

现在,当您在方法中使用类型String时。你应该返回一个字符串,这就是你的编辑所说的有回报的东西。你应该使用循环返回,因为你需要找到最高的学生,这只有在循环所有列表后才可能。

You can try this:

你可以试试这个:

public static String highestGradeName(int[] g, String[] n) {

    int max = 0; 
    int index = 0;
    for (int i = 0; i < g.length; i++) {
        if (max < g[i]) {
            max = g[i];
            index = i;
        }
    }
    return n[index];

}

PS: Imporve code according to mureinik answer, I have one more variable to help you understand easily.

PS:根据mureinik的答案改编代码,我还有一个变量可以帮助您轻松理解。

#1


3  

Returning from inside the loop is wrong, as you can always have an even larger number later on in the array. You should keep the index of the highest grade and just return the corresponding name at the end:

从循环内部返回是错误的,因为稍后在数组中总是可以有更大的数字。您应该保留最高等级的索引,并在最后返回相应的名称:

public static String highestGradeName(int[] g, String[] n) {
    int highest = 0;
    for (int i = 1; i < g.length; i++) {
        if (g[highest] < g[i]) {
            highest = i;
        }
    }
    return n[highest];
}

#2


0  

According to your logic let's break things.

根据你的逻辑,让我们打破局面。

Raw Test Case:

原始测试案例:

Take {0,1,2} as an integer arrays. Take {"arya", "jon", "tyrion"} as an string arrays.

将{0,1,2}作为整数数组。以{“arya”,“jon”,“tyrion”}作为字符串数组。

highest = 0; // according to your code.

For int i = 1, i < 3; i++
0 < 1
highstudent = 2 // tyrion
// returns tyrion

The reason why you are getting null is your integer at 0 index should be greater or equal to the index at 1.

你得到null的原因是你的0整数索引应该大于或等于1的索引。

Now, when you are using a type String in your method. You should return a string and that's what your editor said to have something return. You should use return out of the loop because you need to find the highest student which is only possible after looping all the list.

现在,当您在方法中使用类型String时。你应该返回一个字符串,这就是你的编辑所说的有回报的东西。你应该使用循环返回,因为你需要找到最高的学生,这只有在循环所有列表后才可能。

You can try this:

你可以试试这个:

public static String highestGradeName(int[] g, String[] n) {

    int max = 0; 
    int index = 0;
    for (int i = 0; i < g.length; i++) {
        if (max < g[i]) {
            max = g[i];
            index = i;
        }
    }
    return n[index];

}

PS: Imporve code according to mureinik answer, I have one more variable to help you understand easily.

PS:根据mureinik的答案改编代码,我还有一个变量可以帮助您轻松理解。