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

2011. 6. 3. 15:29

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

출력 결과
helloooo....
helloooo....
helloooo....
helloooo....
10
코드 조각
e1 = e1 + 1;
e1 = count + 1;
e1.count = count + 1;
e1.count = e1.count + 1;
x
y
e2
count
x < 4
x < 5
x > 0
x > 1
Echo
Tester
echo()
count()
hello()
e2 = e1;
Echo e2;
Echo e2 = e1;
Echo e2 = new Echo();
x == 3
x == 4
문제
public class EchoTestDrive {
	public static void main(String[] args){
		Echo e1 = new Echo();
		_________________
		int x = 0;
		
		while(________){
			e1.hello();
			_________________
			
			if(________){
				e2.count = e2.count + 1;
			}
			if________){
				e2.count = e2.count + e1.count;
			}
			x = x + 1;
		}
		System.out.println(e2.count);
	}
}
class _________________
	int _________________ = 0;
	void _________________
		System.out.println("helloooo....");
	}
}
정답
public class EchoTestDrive {
	public static void main(String[] args){
		Echo e1 = new Echo();
		Echo e2 = new Echo();
		int x = 0;
		
		while(x < 4){
			e1.hello();
			e1.count = e1.count + 1;
			
			if(x == 3){
				e2.count = e2.count + 1;
			}
			if(x > 0){
				e2.count = e2.count + e1.count;
			}
			x = x + 1;
		}
		System.out.println(e2.count);
	}
}

class Echo {
	int count = 0;
	void hello(){
		System.out.println("helloooo....");
	}
}

Binple 개발/Java , , ,

  1. Blog Icon

    비밀댓글입니다

  2. Blog Icon

    비밀댓글입니다

  3. Blog Icon

    비밀댓글입니다