Cute Happy Ghost
본문 바로가기
JAVA/Java

20201014_ 15 2차원배열 겉에 외곽선네모합구하기

by JENN_tech7 2020. 10. 14.
728x90
SMALL

 

public class Sample4 {

	public static void main(String[] args) {
		int sum = 0;
		int count = 1;
		int[][] a = new int[5][5];
		
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a.length; j++) {
				a[i][j] = count;
				count ++;
				System.out.printf("%4d" ,a[i][j]);
			}
			System.out.println(" ");
		}
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a.length; j++) {
				if (i== 0 || i ==4|| j==0 || j ==4) {
					sum += a[i][j];
					System.out.println(a[i][j]);
				}
			}
		}
		System.out.println(sum);
	}
}
   1   2   3   4   5 
   6   7   8   9  10 
  11  12  13  14  15 
  16  17  18  19  20 
  21  22  23  24  25 
208

 

728x90
LIST

댓글