0

ユーザーに double 値の nxn 行列を入力するように促し、最初の行列の列が並べ替えられた新しい行列を表示するプログラムを作成します。問題を解決するために、任意のソート アルゴリズムを使用できます。使用するソート アルゴリズムの名前をコード ヘッダーに指定してください。プログラムはソート アルゴリズムを実装する必要があります。Array クラスで提供される並べ替えメソッドは使用できません。並べ替えはメソッドに実装する必要があります。このメソッドでは、新しい配列が返され、元の配列はそのままです。

public static double[][] sortCol(double[][] a)

プログラムは、初期行列と結果行列をユーザーに出力するメソッドも実装する必要があります。印刷物は適切にフォーマットされている必要があります。実行例は次のとおりです。

What is the dimension of matrix? 3 
Enter a 3x3 matrix row by row: 

0.15 0.875 0.375

0.55 0.005 0.225

0.30 0.12 0.4

The column sorted array is: 

0.15 0.005 0.225

0.3 0.12 0.375

0.55 0.875 0.4

これは私が持っているものです。ほぼ完璧だと思います。私が使用した並べ替え方法は、列を並べ替えると思いますが、行を並べ替える場合もあります。しかし、プログラムを実行すると、これが得られます...

スレッド「メイン」での例外 java.util.Scanner.nextDouble( Scanner.java:2456) で Hmwk3_jrgluck.main (Hmwk3_jrgluck.java:16)

任意のアイデア/ヘルプ..

import java.util.Scanner;

public class sdfjasdf {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("What is the dimension of your matrix?");
        int matrixdim = input.nextInt();
        double[][] matrix = new double[matrixdim][matrixdim];

        System.out.println("Enter " + matrixdim + " rows, and " + matrixdim
                + " columns.");
        Scanner input1 = new Scanner(System.in);
        for (int row = 0; row < matrix.length; row++) {
            for (int column = 0; column < matrix.length; column++)
                matrix[row][column] = input1.nextDouble();
        }
        System.out.println(sortCol(matrix));
    }

    public static double sortCol(double[][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            double currentMin = matrix[i][0];
            int currentMinIndex = i;

            for (int j = i; j < matrix.length; j++) {
                if (currentMin > matrix[j][0]
                        || (currentMin == matrix[j][0] && matrix[currentMinIndex][1] > matrix[j][1])) {
                    currentMin = matrix[j][0];
                    currentMinIndex = j;
                }
            }

            if (currentMinIndex != i) {
                double temp0 = matrix[currentMinIndex][0];
                double temp1 = matrix[currentMinIndex][1];
                matrix[currentMinIndex][0] = matrix[i][0];
                matrix[currentMinIndex][1] = matrix[i][1];
                matrix[i][0] = temp0;
                matrix[i][1] = temp1;
            }
        }
        return sortCol(matrix);
    }
}
4

1 に答える 1