관리 메뉴

nalaolla

if~else GAME2 본문

JAVA/3. Control

if~else GAME2

날아올라↗↗ 2015. 12. 20. 16:47
728x90
  1. package test.com;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Test04Game2 {
  6.  
  7.     final static String SID = "aa";
  8.     final static String SPWD = "bb";
  9.     final static String TITLE = "[GAME PROGRAM]";
  10.     final static String LINE = "==============================";
  11.    
  12.     static int loginResult = 0;
  13.     public static void main(String[] args) {
  14.         // TODO Auto-generated method stub
  15.        
  16.         //1. 게임실행
  17.         //2. 로그인 (id = aa, pwd = bb)
  18.         //3. 정상로그인 > 캐릭터 선택, 비정상로그인 > error massage
  19.        
  20.         int startChoice = 0;
  21.        
  22.         System.out.println(TITLE);
  23.         System.out.println(LINE);
  24.        
  25.         while(true) {
  26.            
  27.             showMenu();
  28.             startChoice = keyboard.nextInt();
  29.             keyboard.nextLine();
  30.            
  31.             switch(startChoice) {
  32.                 case 1 :
  33.                     System.out.println("로그인 정보를 입력하세요..");
  34.                     int loginResult = readData();
  35.  
  36.                     if (loginResult == 1) {
  37.                         charSelect();
  38.                         return;
  39.                     } else {
  40.                         System.out.println("로그인실패...");
  41.                         break;
  42.                     }
  43.                    
  44.                 case 2 :
  45.                     System.out.println("게임을 종료합니다. 감사합니다.");
  46.                     return;
  47.                 default :
  48.                     System.out.println("잘못선택");
  49.                     break;
  50.             }
  51.            
  52.         }
  53.     }
  54.    
  55.     public static void showMenu() {
  56.         System.out.println("선택하세요..");
  57.         System.out.println("1. 로그인");
  58.         System.out.println("2. 게임종료");
  59.         System.out.print("입력 : ");
  60.     }
  61.    
  62.     public static int readData() {
  63.        
  64.        
  65.        
  66.         System.out.print("ID : ");
  67.         String id = keyboard.nextLine();
  68.         System.out.print("PWD : ");
  69.         String pwd = keyboard.nextLine();
  70.        
  71.         if (SID.equals(id) && SPWD.equals(pwd)) {
  72.             loginResult = 1;
  73.         }
  74.        
  75.         return loginResult;
  76.        
  77.     }
  78.    
  79.     public static void charSelect() {
  80.         System.out.print("캐릭터선택 : ");
  81.        
  82.         int charChoice = 0;
  83.        
  84.         charChoice = keyboard.nextInt();
  85.        
  86.         if (charChoice == 1) {
  87.             System.out.println("선택캐릭터 >> 인간 - 공격력:5, 방어력:6");
  88.         } else if (charChoice == 2) {
  89.             System.out.println("선택캐릭터 >> 괴물 - 공격력:8, 방어력:3");
  90.         } else if (charChoice == 3) {
  91.             System.out.println("선택캐릭터 >> 로봇 - 공격력:3, 방어력:8");
  92.         } else {
  93.             System.out.println("캐릭터 선택은 1~3번만 가능합니다.");
  94.             charSelect();
  95.         }
  96.     }
  97.    
  98.     static Scanner keyboard = new Scanner(System.in);
  99.  
  100. }


728x90

'JAVA > 3. Control' 카테고리의 다른 글

Switch~Case~Break  (0) 2015.12.20
자바는 블럭단위 처리다.  (0) 2015.12.20
if~else game  (0) 2015.12.20
if~else 자판기2  (0) 2015.12.20
if~else 자판기  (0) 2015.12.20