본문 바로가기

AWS CLOUD FRAMEWORK/Java

[Day06] Position

package loop;

public class Ex06_Position {
	public static void main(String[] args) {
		
		for(int i = 0; i < 5; i++) {
			for(int j = 0; j < 5; j++) {
				System.out.printf("[%d, %d] ", i, j);
			}
			System.out.println(); // 한 줄의 출력이 끝나면 줄바꿈을 넣는다
		}
		System.out.println();
		
		for(int i = 0; i < 5; i++) {
			for(int j = 0; j < 5; j++) {
				if(i == 2 || j == 2) {
					System.out.print("* ");	
				}
				else {
					System.out.print("  ");
				}
			}
			System.out.println();
		}
		System.out.println();
		
		int width = 5;
		int height = 5;
		int halfOfWidth = width / 2;
		int halfOfHeight = height / 2;
		
		for(int i = 0; i < height; i++) {
			for(int j = 0; j < width; j++) {
				if(i + j == width - 1 || i == j) {
					System.out.print("* ");	
				}
				else {
					System.out.print("  ");
				}
			}
			System.out.println();
		}
		System.out.println();
		
		
		for(int i = 0; i < height; i++) {
			for(int j = 0; j < width; j++) {
//				if(i + j == 2 || i + j == 6 || i - j == 2 || j - i == 2) {
//				if(Math.abs(i - j) == 2 || i == j && i % 2 != 0) {
				
				if(Math.abs(halfOfHeight - j) + Math.abs(halfOfWidth - i) == 2) {
					System.out.print("* ");	
				}
				else {
					System.out.print("  ");
				}
			}
			System.out.println();
		}
		System.out.println();
	}
}

'AWS CLOUD FRAMEWORK > Java' 카테고리의 다른 글

[Day06] Quiz2  (0) 2023.03.16
[Day06] LoopCount  (0) 2023.03.16
[Day06] LoopInLoop  (0) 2023.03.16
[Day06] Quiz1  (0) 2023.03.16
[Day06] Ex04  (0) 2023.03.16