1. CLASS
import java.util.*;
public class HIGH_001 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int H = 100;
int L = 0;
int fguess=(H+L)/2;
int A;
int loopCount = 1;
String que = "c";
System.out.println ("Think of a number between "+100+" and "+L+"." );
A = keyboard.nextInt();
while(true){
// System.out.println("Is the number "+ fguess + "\n");
if(fguess > A){
que = "l";
H = H/2;
}else if(fguess < A){
que = "h";
L = H/2;
}else if(fguess == A){
System.out.println("Number of guesses: " + loopCount);
break;
}else{
System.out.println("Only 1 ~ 100 NUMBER");
}
fguess += H - L;
System.out.println("Is the number "+ fguess + " (Correct, Low, High) ? " + que + "\n");
loopCount++;
}
}
}
2. RUN
Think of a number between 100 and 0.
78
Is the number 100 (Correct, Low, High) ? h
Is the number 100 (Correct, Low, High) ? l
Is the number 75 (Correct, Low, High) ? l
Is the number 88 (Correct, Low, High) ? h
Is the number 88 (Correct, Low, High) ? l
Is the number 82 (Correct, Low, High) ? l
Is the number 73 (Correct, Low, High) ? l
Is the number 75 (Correct, Low, High) ? h
Is the number 77 (Correct, Low, High) ? h
Is the number 79 (Correct, Low, High) ? h
Is the number 79 (Correct, Low, High) ? l
Is the number 78 (Correct, Low, High) ? l
Number of guesses: 13
'course > 지식인' 카테고리의 다른 글
[자바] HashSet 에서 특정 자리의 숫자 추출 (0) | 2015.12.19 |
---|---|
[자바] 문자열을 받아 한문자씩 출력(문자열 -> 문자) (0) | 2015.12.08 |
[자바스크립트] 주민번호 검증 스크립트 (0) | 2015.12.07 |