2016-07-05 30 views
-3

文字列値を含む行と整数値を含む行を持つ多次元配列を持っています。私はuが任意の提案や答えを持っているのであれば、UM多次元配列で最大値を見つける方法

を助けてください
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

um私はdunno ????? –

+0

@ KarryAlamsあなたはストリング[st_num] [2]をする必要があります。インデックスは0から始まるため、1ではなく2を使用します。ただし、配列を初期化するときは、ゼロからではなく1から数値を入力する必要があります。そして、このhttp://stackoverflow.com/questions/38191408/how-to-sort-multidimensional-string-array-by-one-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は、Stream がprivate sortByScoreメソッドのString [] [] ...に従うように存在します。 –

+0

@creativecreatormaybenotはそれほど気にしませんが、あなたのコードはまだ問題を解決しません。独自のプログラムでは、ユーザーが最も高いスコアを最初に入力するのではなく、スコアを並べ替えることができます。だからそれは助けになっていないない –

+0

ああ笑は、間違いを発見した。私はコードを変更しました! arr = sortByScore(arr);を実行する必要があります。 sortByScore(arr)の代わりに使用します。申し訳ありませんが、今は完璧に動作します。 – creativecreatorormaybenot

関連する問題