2016-07-05 67 views
-3

我有一個多維數組,其中包含字符串值的行和列包含整數值。我想找到這在列這種情況下,最大比分的最大值如何在多維數組中找到最大值

這裏是我到目前爲止的代碼(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(); 
    } 
} 

所以,嗯如果u有任何建議或答案,請幫助

+0

比方說,我請你告訴我,最多的是什麼表{5,3,2,4,1,9}。你會如何一步步確定這一點? – Compass

+0

嗯我不知道? –

+0

@KarryAlams你必須做String [st_num] [2]; 2而不是1,因爲索引從0開始,但在初始化數組時,必須從1輸入數值而不是從0輸入數值。然後請參閱這個http://stackoverflow.com/questions/38191408/how-to-sort-multidimensional-string-array-by-column-in-integer-value-in-java問題。我有和你一樣的問題,並得到回答。 – creativecreatorormaybenot

回答

0

你必須像這樣改變你的代碼,我會在底部添加一個方法,以便你可以對它進行排序。我將添加想着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 
+0

我不斷收到錯誤消息不兼容的類型:沒有實例類型變量T存在,以便流符合String [] [] ...在私人sortByScore方法 –

+0

不要緊,錯誤@creativecreatormaybenot我修復了這個問題,但是你的代碼仍然不能解決問題。該計劃本身應該能夠安排得分,而不是依靠用戶首先進入最高分。所以沒有它沒有幫助 –

+0

哦哈哈發現了錯誤。我改變了代碼!你必須做arr = sortByScore(arr);而不是僅僅sortByScore(arr)。對不起,我的不好,但它現在工作得很好。 – creativecreatorormaybenot

相關問題