이 질문을 다시 게시하려고하지만 자세한 설명이 필요합니다. 아래 코드를 볼 때 각 열과 각 행의 합계를 출력하고 싶습니다. 행 합계는 해당 행의 마지막 요소 오른쪽에 있어야하며 열 합계는 주어진 열의 마지막 요소 아래에 있어야합니다. 제 출력물을 원한다면 프로그램 시작 부분에있는 주석을보십시오. 어떻게이 일을 할 수 있습니까? 또한 주어진 사용자 입력 배열의 주요 대각선을 인쇄하고 싶습니다. 아래 코드에서 주 대각선은 {1,3,5}로 출력됩니다. 고맙습니다!Java에서 2D 배열로 작업
/*
1 2 3 Row 0: 6
2 3 4 Row 1: 9
3 4 5 Row 2: 12
Column 0: 6
Column 1: 9
Column 2: 12
*/
import java.util.Scanner;
import java.util.Arrays;
public class Test2Darray {
public static void main(String[] args) {
Scanner scan =new Scanner(System.in); //creates scanner object
System.out.println("How many rows to fill?"); //prompts user how many numbers they want to store in array
int rows = scan.nextInt(); //takes input for response
System.out.println("How many columns to fill?");
int columns = scan.nextInt();
int[][] array2d=new int[rows][columns]; //array for the elements
for(int row=0;row<rows;row++)
for (int column=0; column < columns; column++)
{
System.out.println("Enter Element #" + row + column + ": "); //Stops at each element for next input
array2d[row][column]=scan.nextInt(); //Takes in current input
}
for(int row = 0; row < rows; row++)
{
for(int column = 0; column < columns; column++)
{
System.out.print(array2d[row][column] + " ");
}
System.out.println();
}
System.out.println("\n");
}
}
}
편집하는 대신 같은 문제에 대한 새로운 질문을 게시의이 질문 http://stackoverflow.com/questions/21820588/summing-up-a-2d-array. –