본문 바로가기
코딩수업/AWS 클라우드환경 네이티브

7/26 자바(Java) 반복문 연습 문제

by 인생즐겜러 2022. 7. 26.
728x90
반응형

AWS 클라우드환경 네이티브 수업 51일차

 

 

 

진행

1. 오늘도 연습 문제

 

 

 

 

 

요약

1. 반복문 연습 문제

 

 

 

 

 


 

 

 

 

 

문제15-1. 아래의 모양을 만드세요

 

★★★★★
★★★★
★★★
★★
★
============================

 

문제15-2. 별(★)을 사용해서 해당 모양(별 모양)을 출력해 보세요.

문제16. 아래의 모양을 만드세요.

(한줄씩 설정해서 만드는 방식 / 간단하게 각 행과 열의 좌표를 이용하는 방식)

 

★☆☆☆★
☆★☆★☆
☆☆★☆☆
☆★☆★☆
★☆☆☆★

 

 

 

 

 


 

 

 

 

 

문제15-1 답.

package LectureExam;

public class exercise2 {

	public static void main(String[] args) {

	
		for(int i = 1 ; i < 6; i++ ) {
			for(int j = 1 ; j < 7 - i ; j++) {
				System.out.print("★");
			}
			System.out.println();
		}
		
		System.out.println("============================");
		
	}
}

 

 

 

문제15-2 답.

package LectureExam;

public class exercise2 {

	public static void main(String[] args) {

		// 윗단
		for(int a = 1 ; a < 4 ; a++) {
			for(int z = 1 ; z < 7-a ; z++ ) {
				System.out.print(" ");				
			}
			for(int y = 1 ; y < 2*a ; y++ ) {
				System.out.print("★");				
			}
			for(int x = 1 ; x < 7-a ; x++ ) {
				System.out.print(" ");				
			}

			System.out.println();
		}
		
		// 중단
		for(int b = 1 ; b < 4 ; b++) {
			
			
			if(b==1) {
				for(int v = 0 ; v < 12 ; v++ ) {
					System.out.print("★");
				}
				System.out.println();
				
			} else {
				
				for(int w = 1 ; w < 1+b ; w++ ) {
					System.out.print(" ");
				}
				for(int v = 0 ; v < 11-2*b ; v++ ) {
					System.out.print("★");
				}
				for(int u = 1 ; u < 1+b ; u++ ) {
					System.out.print(" ");
				}
				
				System.out.println();
			}
			

		}

		// 하단
		for(int c = 1 ; c < 3 ; c++) {
			for(int t = 1 ; t < 4-c ; t++ ) {
				System.out.print(" ");
			}
			for(int s = 0 ; s < 4-c ; s++ ) {
				System.out.print("★");
			}
			for(int r = 1 ; r < 4*c-2 ; r++ ) {
				System.out.print(" ");
			}
			for(int q = 0 ; q < 4-c ; q++ ) {
				System.out.print("★");
			}
			for(int p = 1 ; p < 4-c ; p++ ) {
				System.out.print(" ");
			}
			
			System.out.println();
		}
	}
}



결과
         ★     
        ★★★    
      ★★★★★   
★★★★★★★★★★★★
    ★★★★★★★  
      ★★★★★   
     ★★★ ★★★  
    ★★       ★★

 

대체 왜 찌그려져서 출력되는지 모르겠지만...

아무튼 프로그램으로 돌리면 정상적으로 잘 나온다.

 

 

 

문제16-1 답.

package LectureExam;

public class exercise2 {

	public static void main(String[] args) {

	
		for(int i = 1 ; i < 6; i++ ) {
			
			// 첫 줄
			if(i>=4) {
				for(int a = 1 ; a < 6 - i ; a++) {
					System.out.print("☆");
				}
			}
			else {
				for(int a = 1 ; a < i ; a++) {
					System.out.print("☆");
				}
			}
				
			// 둘째 줄
			System.out.print("★");
			
			// 셋째 줄
			if(i<=2) {
				for(int a = 1 ; a < 6 - 2*i ; a++) {
					System.out.print("☆");
				}
			}
			else if(i==3){
				System.out.print("☆☆");
			} else {
				for(int a = 1 ; a < 2*i-6 ; a++) {
					System.out.print("☆");
				}
			}

			// 넷째 줄
			if(i == 3 ) {
				System.out.print(" ");
			} else {
				System.out.print("★");
			}
			
			// 다섯째 줄
			if(i%2==1) {
				System.out.print(" ");
			} else {
				System.out.print("☆");
			}

			System.out.println();
		}
	}
}



결과
★☆☆☆★
☆★☆★☆
☆☆★☆☆
☆★☆★☆
★☆☆☆★

 

문제16-2 답.

package LectureExam;

public class teacherAns {

	public static void main(String[] args) {

			
		for(int i = 0 ; i < 5 ; i++){
			for(int j = 0 ; j < 5 ; j++ ) {
				
				if(i == j || i + j == 4) {
					System.out.print("★");
				} else {
					System.out.print("☆");
				}
							
			}
			System.out.println();
		}
	}
}



이유
(0,0)(0,1)(0,2)(0,3)(0,4)
(1,0)(1,1)(1,2)(1,3)(1,4)
(2,0)(2,1)(2,2)(2,3)(2,4)
(3,0)(3,1)(3,2)(3,3)(3,4)
(4,0)(4,1)(4,2)(4,3)(4,4)

 

위의 이유를 참고해서 확인해보면

검은 별이 있는 좌표의 규칙성이 if문의 조건이다.

 

 

 

 

 

728x90
반응형

댓글