Created
November 26, 2016 15:57
-
-
Save JoeUnsung/7f9aae1b790f4109ecdcb68c36a03428 to your computer and use it in GitHub Desktop.
Try to get answer number between 1 and 100.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package joe; | |
import java.util.*; | |
public class HW3_20101062_1 { | |
public static void main(String[] args){ | |
// 1~100 사이의 임의의 값을 얻어서 answer 에 저장한다. | |
int cnt = 0, input = 0; | |
int answer = (int)(Math.random() * 100) + 1; | |
Scanner scan = new Scanner(System.in); | |
while(true){ | |
cnt++; | |
System.out.println("1과 100 사이의 값을 입력하세요"); | |
input = scan.nextInt(); | |
if (answer == input){ | |
break; | |
} | |
else if ( answer > input ){ | |
System.out.println("더 큰 수를 입력하세요."); | |
} | |
else if ( answer < input ){ | |
System.out.println("더 작은 수를 입력하세요."); | |
} | |
else{ | |
continue; | |
} | |
} | |
System.out.println("시도 횟수는 " + cnt +" 번 입니다."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment