1

この 1 つのエクササイズを 3 日間解こうとしてきましたが、うまくいきません。アイデアは、1 で増加するマトリックスを作成することです。プログラムは行と列のサイズを要求し、行列を作成します。

期待される結果の例を示します。

1   2   3   4
5   6   7   8

ここに私が得るものがあります:

1   1   1   1
1   1   1   1

そして、ここに私のコードがあります:

        public static void main (String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader
                        (System.in));

        // User enters row and column size of the 2D array.
        System.out.print ("Input row size: ");
        int ro = Integer.parseInt(in.readLine());

        System.out.print ("Input column size: ");
        int co = Integer.parseInt(in.readLine());
        System.out.println ("   Array is " + ro + "x" + co);

        // Creating a 2D array.
        int[][] a = new int[ro][co];

        // User enters values, which are put into the array. 
        // This is the part where the code fails.
        System.out.print ("The matrix has " + ro*co + " integer values");
        System.out.println (" (one per line): ");
        for (int r = 0; r < a.length; r++)
            for (int c = 0; c < a[0].length; c++) {
                a [r][c] +=1;
            }

        // Printing the matrix
        System.out.println ("Matrix:");
        for (int r = 0; r < a.length; r++) {
            for (int c = 0; c < a[0].length; c++)
                System.out.print (a[r][c] + " ");
            System.out.println();
        }

        }
4

4 に答える 4

5

インクリメントするには、ループの外側に変数が必要です。

int incr = 0;

ループ内で、これを行います

a [r][c] = ++incr;

現在、 initally である配列内の各要素をインクリメントしている0ため、0+1常に 1 になります。

于 2013-07-24T09:08:19.980 に答える
0

public static void main(String[] args) throws IOException {

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    // User enters row and column size of the 2D array.
    System.out.print("Input row size: ");
    int ro = Integer.parseInt(in.readLine());

    System.out.print("Input column size: ");
    int co = Integer.parseInt(in.readLine());
    System.out.println("   Array is " + ro + "x" + co);

    // Creating a 2D array.
    int[][] a = new int[ro][co];

    // User enters values, which are put into the array.
    // This is the part where the code fails.
    System.out.print("The matrix has " + ro * co + " integer values");
    System.out.println(" (one per line): ");
    //For incremental purpose
    int counter = 0;
    for (int r = 0; r < a.length; r++)
        for (int c = 0; c < a[0].length; c++) {
            a[r][c] = ++counter;
        }

    // Printing the matrix
    System.out.println("Matrix:");
    for (int r = 0; r < a.length; r++) {
        for (int c = 0; c < a[0].length; c++)
            System.out.print(a[r][c] + " ");
        System.out.println();
    }

}
于 2013-07-24T09:15:31.630 に答える
0

Arrayオブジェクトはオブジェクトであり、オブジェクトのデータメンバーはデフォルト値を取得するため、ループの外側でインクリメントする必要があるため0、配列の各要素に割り当てられます。

int inc = 0;

そしてループ内

a[r][c]=inc++;
于 2013-07-24T09:15:08.993 に答える
0

ループは単に配列を 1 ずつインクリメントします。すべての配列要素はゼロから始まるため、すべてが 1 ずつ増加するため、結果が得られます。次のように、ループの外側に変数を含めてみてください。

int i = 0;

次に、ループ内の行を次のように変更します

i++;
a[r][c] = i;
于 2013-07-24T09:11:55.200 に答える