1

そこで、ユーザーに 5 つのジャンルを入力して 10 点満点で採点するように求める簡単なプログラムを作成しました。検証は追加していませんが、それについては心配していません。ご覧のとおり、 と の 2 つの配列がgenres[]ありscore[]ます。彼らが入力するとしましょう:

        [1] : Genre A | 3
        [2] : Genre B | 6
        [3] : Genre C | 2
        [4] : Genre D | 10
        [5] : Genre E | 8

結果をリストする必要があります ジャンル D、E、B、A、C

これが私の全体的なコードです:

import java.util.Scanner;

class OrigClass {
    public static void main (String[] args){
        Scanner ScanObj = new Scanner(System.in);
        int count;
        String[] genres;
        genres = new String[5];
        int[] score;
        score = new int[5];
        for (count = 0; count < 5;count++){
            System.out.println("Enter A Genre: ");
            genres[count] = ScanObj.nextLine();
            System.out.println("How much do you like it? ");
            score[count] = ScanObj.nextInt();
            ScanObj.nextLine();
        }

        for (count = 0; count < 5; count++){
            System.out.println(count+1 + ") " + genres[count] + " " + score[count] + "/10");

        }
        ScanObj.close();
    }
}

いくつかの関数を使用してJavaでそれを行う賢い方法はありますか、それとも一時変数、ifステートメントなどを使用して手動で行う必要がありますか?実装がかなり簡単なバブルソートアルゴリズムも使用できると思います。score[]そのため、内容を降順に並べ替えたいと思います。ヒントをいただければ幸いです。

4

3 に答える 3