JAVA 좌우 지그재그 출력

EUGENE LEE·2022년 6월 28일
0

1부터 n * n까지 규칙에 맞춰 적었을 때의 결과를 n개의 줄에 걸쳐 출력합니다.
각 줄에는 각 행에 해당하는 n개의 숫자를 공백을 사이에 두고 출력합니다.

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt(); 
	int cnt = 1;
	for(int i=0; i<n;i++) {
		for(int j=0; j<n; j++) {
			if(i%2 == 0) {
				System.out.print(cnt+" ");
				cnt++;
			}
			if(i%2 !=0) {
				cnt--;
				System.out.print(cnt+" ");
			}
		}
		System.out.print("\n");
		cnt +=n; //다음줄로 넘어갈때 n씩 증가
	}
}

}

profile
개발공부

0개의 댓글