Я пытался решить это упражнение в течение трех дней, но я не получу его. Идея заключается в создании матрицы, которая увеличивается с одной. Программа запрашивает размер строк и столбцов, а затем создает матрицу.Проблемы с приращением 2-мерного массива java
Даю пример ожидаемого результата:
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();
}
}
спасибо чувак за очень быструю реакцию! – Developous