公費日06:複文、ラベル、中断、継続



🌟例1🌟


九九段出力
public class Test01 {
	public static void main(String[] args) {
		int dan = 7;
		
		for(int i = 0; i < 9 ; i++) {
			System.out.println(
            	dan + " x " + (i + 1) + " = " + (dan * (i + 1))
            );
		}
	}
}

🌟例2🌟

public class Test02 {

	public static void main(String[] args) {
		int[] num = {1, 2, 3, 4, 5, 6};

		int len = num.length;	
        // index와 다르게 length는 1부터
		
		for(int i = 0 ; i < len ; i++) {
			System.out.println(
            	(i + 1) + " 번째 데이터 : " + num[i]
            );			
		}
		
		// 향상된 for문으로 처리
		for(int no : num) {
			System.out.println("### " + no);
		}
	}
}

🌟例3🌟

public class Test03 {
	public static void main(String[] args) {
		// 1 ~ 숫자를 만들어서 출력하고 50이 되면 반복을 종료하세요.
		
		for(int i = 0 ;; i++) {
			int no = i + 1;
			if(no > 50) {
				break;
			}
			System.out.print(no + ", ");
		}
		System.out.println();
		
		int no = 1;
		while(true) {		
        // 무한반복 while문의 경우 true를 넣으면 됨.	
			if(no > 50) break;
			System.out.print(no + ", ");
			++no;
		}	
	}
}

🌟例4🌟

public class Test04 {
	public static void main(String[] args) {
		int no = 10;
		
		do {
			System.out.println("no : " + no);
		} while(++no < 10);
	}
}

🌟例5🌟

public class Test05 {
	public static void main(String[] args) {
		// 구구단 5단을 출력하는데 곱이 5인 경우는 제외하고 출력하세요.
		int dan = 5;
		
		for(int i = 0 ; i < 9 ; i++) {
			int gop = (i + 1);
			
			if(gop == 5) {
				continue;
			}
			
			System.out.println(
            	dan + " x " + gop + " = " + (dan * gop)
            );
		}
	}
}

🌟例6🌟

public class Test06 {
	public static void main(String[] args) {
		loop:	// 레이블, i를 카운터변수로 쓰는 반복문의 이름
		for(int i = 2 ; i < 10 ; i++) {
			for(int j = 1 ; j < 10 ; j++) {
				if(i == 5 && j == 3) {
					break loop;  // loop라는 반복문 즉시 종료
				}
				System.out.println(i + " x " + j + " = " + (i * j));
			}
		}
	}
}

練習問題


👑練習問題1👑


九九段出力
public class Ex01 {
	public static void main(String[] args) {
		for(int i = 1; i <= 9; i++) {
			for(int j = 2; j <= 5; j++) {
				System.out.print(j + " x " + i + " = " + (j * i) +"\t");
			}
			System.out.println();
		}
		
		for(int i = 1; i <= 9; i++) {
			for(int j = 6; j <= 9; j++) {
				System.out.print(j + " x " + i + " = " + (j * i) + "\t");
			}
			System.out.println();
		}
	}
}

👑練習問題👑


二人で山に登ります.
一人で山口で0.54 m/sの速さで登り始めました.
一人で山頂から1.07メートル/秒で降りた.
山の高さは7564メートルだと仮定します.
二人の面会時間は何時何分何秒後ですか.
public class Ex02 {
	public static void main(String[] args) {
		// 산 높이 정수 변수에 저장
		int height = 7564;
		
		// for 반복문으로 두 사람이 만나는 시점 구하기
		// 몇초 걸리는지 구해 분초로 만들거임
		int i = 1;
		
		// 두 사람의 속도를 변수에 저장
		double a = 0.54, b = 1.07;
		
		for(i = 1 ; ; i++) {
			int result = (int)((a * i) + (b * i));
			
			if(result >= height) {
				break;
			}
		}
		
		// i는 시간을 모두 초로 받은 변수이므로 계산으로 시 분 초를 구한다.
		int hour = i / (60 * 60);
		int min = (i / 60) % 60;
		int sec = i % 60;
		
		System.out.println("두 사람이 만나는 때는 출발부터 " +
        	hour + "시간 " + min + "분 " + sec + "초 지났을 때이다."
        );
	}
}

👑練習問題👑

public class Ex03 {
	public static void main(String[] args) {
		/*
		 	1 1 1 1 1 
			2 2 2 2 2 
			3 3 3 3 3 
			4 4 4 4 4 
			5 5 5 5 5 
		 */
		for(int j = 0 ; j < 5 ; j++) {
			for(int i = 0 ; i < 5 ; i++) {
				System.out.print((j + 1) + " ");
			}
			System.out.println();
		}
		
		/*
		 	1 2 3 4 5
			1 2 3 4 5
			1 2 3 4 5
			1 2 3 4 5
			1 2 3 4 5
		 */
		for(int j = 0 ; j < 5 ; j++) {
			for(int i = 0 ; i < 5 ; i++) {
				System.out.print((i + 1) + " ");
			}
			System.out.println();
		}

		/*
		 	1 2 3 4 5
			2 3 4 5 6 
			3 4 5 6 7
			4 5 6 7 8 
			5 6 7 8 9 
		 */
		for(int j = 0 ; j < 5 ; j++) {
			for(int i = 0 ; i < 5 ; i++) {
				System.out.print((i + 1 + j) + " ");
			}
			System.out.println();
		}
		
		/*
		 	 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
		 */
		for(int j = 0 ; j < 5 ; j++) {
			for(int i = 0 ; i < 5 ; i++) {
				if(i + 1 + (5 * j) < 10) {
					System.out.print(" " + (i + 1 + (5 * j)) + " ");
				} else {
					System.out.print((i + 1 + (5 * j)) + " ");
				}
			}
			System.out.println();
		}
		
		/*
		 	1
			1 2
			1 2 3
			1 2 3 4
			1 2 3 4 5
		 */
		for(int j = 0 ; j < 5 ; j++) {
			for(int i = 0 ; i < j + 1 ; i++) {
				System.out.print((i + 1) + " ");
			}
			System.out.println();
		}
		
		/*
		 	 1
			 2  3
			 4  5  6
			 7  8  9 10
			11 12 13 14 15
		 */
		int a = 0;
		for(int j = 1 ; j < 6 ; j++) {
			for(int i = 1 ; i < j + 1 ; i++) {
				if((i + a) < 10) {
					System.out.print(" " + (i + a) + " ");
				} else {
					System.out.print((i + a) + " ");
				}
			}
			a += j;
			System.out.println();
		}
	}
}

👑練習問題👑

public class Ex04 {
	public static void main(String[] args) {
		/*
		 	*****
			*****
			*****
			*****
			*****
		 */
		for(int j = 0 ; j < 5 ; j++) {
			for(int i = 0 ; i < 5 ; i++) {
				System.out.print("*");
			}
			System.out.println();
		}
		
		/*
		 	*****
			****
			***
			**
			*
		 */
		for(int j = 0; j < 5 ; j++) {
			for(int i = 0 ; i < 5 - j ; i++) {
				System.out.print("*");
			}
			System.out.println();
		}
		
		/*
		 	    *
			   **
			  ***
			 ****
			*****
		 */
		for(int j = 1 ; j < 6 ; j++) {
			for(int i = 0 ; i < 5 - j ; i++) {
				System.out.print(" ");
			}
			
			for(int i = 0 ; i < j ; i++) {
				System.out.print("*");
			}
			System.out.println();
		}
		
		/*
		 	  *
			 ***
			*****
		 */
		for(int i = 0 ; i < 3 ; i++) {
			for(int j = 2 ; j > i ; j--) {
				System.out.print(" ");
			}
			
			for(int j = 0 ; j < (2 * i) + 1 ; j++) {
				System.out.print("*");
			}
			
			for(int j = 2 ; j > i ; j--) {
				System.out.print(" ");
			}
			System.out.println();
		}
	}
}