-
[JAVA연습] Up&Down NumberGame //숨은 숫자를 맞추는 게임카테고리 없음 2022. 11. 8. 17:20
import java.util.Scanner;
public class Exer2 {
private static int comNum;
private static int min;
private static int max;
private static int count;
public static void main(String[] args) {
// 숨은 숫자를 맞추는 게임을 만들어보자
// 컴퓨터에게 0에서 99까지의 임의의 숫자를 선택하고, 사용자는 이 숫자를 맞추는 게임이다.
Scanner sc = new Scanner(System.in);
int guessNum = 0;
char replay;
boolean restart = true;
System.out.println("정답 : " + comNum);
System.out.println("숫자를 입력해주세요");
System.out.println(min + " - " + max);
createGame();
while (restart) {
count++;
System.out.println(count+"번째 기회입니다. "+">>");
guessNum = sc.nextInt();
if (guessNum < comNum) {
min = guessNum;
System.out.println("입력한 숫자가 더 작습니다.");
System.out.println(min + " - " + max);
} else if (guessNum > comNum) {
max = guessNum;
System.out.println("입력한 숫자가 더 큽니다.");
System.out.println(min + " - " + max);
}else {
System.out.println("정답입니다.");
System.out.println("다시 하시겠습니까? (y/n)");
replay = sc.next().charAt(0);
if(replay == 'y'){
createGame();
}else {
System.out.println("게임을 종료합니다.");
restart = false;
sc.close();
}
}
} //End of while
} //end of main
public static void createGame() {
comNum = (int) Math.floor(Math.random() * 100);
min = 0;
max = 99;
count = 0;
System.out.println("정답 : " + comNum);
System.out.println("숫자를 입력해주세요>>");
System.out.println(min + " - " + max);
}
} //end of Class