[Head First Java] Chapter1 연습문제 수영장 퍼즐

2011. 6. 2. 10:34

코드 조각을 한번만 사용하여 아래와 같은 출력 결과를 얻어야 한다.

출력 결과
a noise
annoys
an oyster
코드 조각
System.out.print(" ");
System.out.print("a");
System.out.print("n");
system.out.print("an");
x > 0
x < 1
x > 1
x > 3
x < 4
System.out.print("noys");
System.out.print("oise");
System.out.print(" oyster");
System.out.print("annoys");
System.out.print("noise");
문제
public class PoolPuzzleOne {
	public static void main(String[] args) {
		int x = 0;
		while(________){
			__________________________
			if(x < 1){
				___________________________
			}
			__________________________
			if(x > 1){
				___________________________
				________
			}
			if( x == 1){
				___________________________
			}
			if( ________){
				___________________________
			}
			System.out.println(" ");
			________
		}
	}

}
정답
public class PoolPuzzleOne {
	public static void main(String[] args) {
		int x = 0;
		while(x < 4){
			System.out.print("a");
			if(x < 1){
				System.out.print(" ");
			}
			System.out.print("n");
			if(x > 1){
				System.out.print(" oyster");
				x = x + 2;
			}
			if( x == 1){
				System.out.print("noys");
			}
			if( x < 1){
				System.out.print("oise");
			}
			System.out.println(" ");
			x = x + 1;
		}
	}

}

Binple 개발/Java , ,