我有一个包含String值的行的多维数组,列包含整数值。我希望在这种情况下找到最大值,即列
中的最大分数这是我目前为止的代码(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();
}
}
所以,如果你有任何建议或答案,请帮助
答案 0 :(得分:0)
你必须像这样更改你的代码,我将在底部添加一个方法,以便你可以对它进行排序。我也会添加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