기존거 달라진건 없고 try ~ catch절만 더해본 것

간만에 다시 코드 써보는데

다시봐도 혼자 못 만들겠다.... 어려워 ㅠㅠㅠ 

 

 

public class test1010 {

 

public static final int SCISSORS =1;

public static final int ROCK =2;

public static final int PAPER =3;

public static final int QUIT =9;

public static final int REPLAY =4;

 

public static final char EXIT = 'E';

public static final char CONTINUE = 'C';

public static final char NORMAL = 'N';

 

static void showMenu() {

System.out.println("===============[메뉴 번호]==================");

System.out.println("        1.가위 2.바위 3.보 4.리플레이 9.종료");

System.out.println("==========================================");

    }

 

 

private static int getComputerNumber(int start , int end) {

return (int)(Math.random()*(end-start)+1)+start;

}

 

private static int getUserNumber() {

Scanner sc = new Scanner(System.in);

while(true)

try {

System.out.println("메뉴번호를 입력하세요?");

return sc.nextInt();

} catch(InputMismatchException e) {

System.out.println("숫자만입력");

return REPLAY;   //??

}

}

 

   static char checkMenu(int menu) {

   if(menu == QUIT) return EXIT;

   else if ( menu == REPLAY) {

   showMenu(); return CONTINUE;

        }

   else if (!(menu ==1 || menu ==2 || menu ==3 )) {

    System.out.println("메뉴에 없는 번호입니다.");

    showMenu();

    return CONTINUE;

   }

    return NORMAL;

}

 

 

 

public static void main(String[] args) {

    showMenu();

    while(true) {

    int computer  = getComputerNumber(1,3);

int user = getUserNumber();

// 메뉴체크메소드

char checks = checkMenu(user);

if(checks == EXIT ) break;

else if (checks == CONTINUE) continue;

     showResult(computer,user);   

    }////////while

System.out.println("다음에 만나요...");

}

 

static String getRPSvalue(int value) {

switch(value) {

case SCISSORS : return "가위";

case ROCK : return "바위";

default :return "보";

}

}////getRPS

private static void showResult(int computer, int user) {

System.out.printf("당신:%s, 컴퓨터:%s%n",getRPSvalue(user),getRPSvalue(computer) );

if(user == SCISSORS && computer == PAPER ||

user == ROCK && computer == SCISSORS||

user == PAPER && computer ==ROCK)

System.out.println("당신이 이겼어요");

else if (user == computer)System.out.println("비겼어요");

else System.out.println("당신이 졌어요");

 

}

}

+ Recent posts