如何在多维数组中找到最大值

凯里·阿拉姆斯(Karry Alams)

我有一个多维数组,其中的行包含字符串值,而列则包含整数值。我想在这种情况下找到列中的最大值

到目前为止,这是我的代码(JAVA)

public class HighScore {
    int row;
    int col;
    Scanner input = new Scanner(System.in);
    public void maxscore() {
        System.out.println("How many students are you entering scores for");
        int st_num = input.nextInt();
        String[][] arr = new String[st_num][1];
        for (row = 0; row < arr.length; row++) {
            System.out.println("Please enter the student's name");
            String name = input.next();
            for (col = 0; col < arr[row].length; col++) {
                System.out.println("Please enter the students's score");
                int score = input.nextInt();
            }
        }
        System.out.println("Highest score entered was");
    }
    public static void main(String[] args) {
        HighScore obj = new HighScore();
        obj.maxscore();
    }
}

所以,如果您有任何建议或答案,请帮助

创造者或可能不是

您必须像这样更改代码,我将在底部添加一个方法,以便您可以对其进行排序。我也将添加missin sysout:

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class HighScore {

    int row;

    int col;

    Scanner input = new Scanner(System.in);

    public void maxscore() {
        System.out.println("How many students are you entering scores for");
        int st_num = input.nextInt();
        String[][] arr = new String[st_num][2]; //you need two columns for Student names AND score
        for (row = 0; row < arr.length; row++) {
            System.out.println("Please enter the student's name");
            arr[row][0] = input.next(); //puts the student name into the first column in every row you have

            System.out.println("Please enter the students's score");
            arr[row][1] = Integer.toString(input.nextInt()); //puts the score into the second column of the row and you need to cast the int to a string
        }
        System.out.println("All scores listed. Highest value at the top: ");

        arr = sortByScore(arr); //sorts the array with created sort method

        for(String[] s : arr) { //goes through the array after its sorted and prints it out
            System.out.println("Students name: " + s[0]);
            System.out.println("Students score: " + s[1]);
        }

        String[][] topScore = new String[1][2]; //will just have the top score

        for(int i = 0; i < topScore.length; i++) { //just goes through one time anyways and then puts the top score onto the topScore array
            topScore[i][0] = arr[0][0]; //the first value is the highest so it takes 0 index
            topScore[i][1] = arr[0][1];
        }



        System.out.println("\nHighest score: ");

        for(String[] s : topScore) { //puts out highest score
            System.out.println("Best students name: " + s[0]);
            System.out.println("Best students score: " + s[1]);
        }
    }
    public static void main(String[] args) {
        HighScore obj = new HighScore();
        obj.maxscore();
    }

    private String[][] sortByScore(String[][] in) {
        String[][] out = Arrays.stream(in) //this uses java 8 streams and takes the in[][] which is in your case the array "arr"
            .sorted(Comparator.comparing(x -> -Integer.parseInt(x[1]))) //sorts it
            .toArray(String[][]::new); //puts it onto the out array

            return out; //and returns the out array back
    }
}

希望对您有所帮助!

控制台日志示例:

How many students are you entering scores for
5
Please enter the student's name
Jay
Please enter the students's score
10
Please enter the student's name
Peet
Please enter the students's score
102
Please enter the student's name
John
Please enter the students's score
52
Please enter the student's name
Zack
Please enter the students's score
1
Please enter the student's name
Fen
Please enter the students's score
95
All scores listed. Highest value at the top: 
Students name: Peet
Students score: 102
Students name: Fen
Students score: 95
Students name: John
Students score: 52
Students name: Jay
Students score: 10
Students name: Zack
Students score: 1

Highest score: 
Best students name: Peet
Best students score: 102

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档