2016-07-05 7 views
-3

문자열 값이 포함 된 행과 정수 값을 포함하는 행이있는 다차원 배열이 있습니다. 나는다차원 배열에서 최대 값을 찾으려면 어떻게합니까

여기

하는 u는 어떤 제안 또는 답변이 경우

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(); 
    } 
} 

그래서 음

도와주세요 (JAVA) 지금까지 내 코드 열에이 경우 최대 점수의 최대 값을 찾으려면
+0

의 내가 가장 높은 번호가 목록에 무엇을 말해하도록 요청한다고 가정 해 봅시다 {5, 3, 2, 4, 1, 9}. 단계별로 어떻게 결정합니까? – Compass

+0

음 I dunno ????? –

+0

@KarryAlams 당신은 문자열 [st_num] [2]; 인덱스가 0부터 시작하기 때문에 1 대신 2를 사용하지만 배열을 초기화 할 때는 0이 아닌 1부터 숫자 값을 입력해야합니다. 그런 다음 http://stackoverflow.com/questions/38191408/how-to-sort-multidimensional-string-array-by-one-column-in-integer-value-in-java 질문을 참조하십시오. 나는 당신이 가진 것과 똑같은 질문을했고 대답했습니다. – creativecreatorormaybenot

답변

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 
+0

에러 메시지가 계속해서 호환되지 않는 타입을 얻는다. 타입 변수 T는 Stream 이 private sortByScore 메소드 –

+0

에 String [] [] ...을 따르도록 존재합니다. @creativecreatormaybenot 오류는 신경 쓰지 마세요. 그러나 수정 한 코드는 여전히 문제를 해결하지 못합니다. 자체적으로 프로그램은 사용자가 가장 높은 점수를 먼저 입력하지 않고 점수를 정렬 할 수 있어야합니다. 그래서 아무 도움이되지 않았습니다 –

+0

오 하하는 실수를 발견했습니다. 코드를 변경했습니다! 당신은 arr = sortByScore (arr); 그냥 sortByScore (arr) 대신에. 미안하지만, 이제는 완벽하게 작동합니다. – creativecreatorormaybenot

관련 문제