본문 바로가기
MEMO/Java-memo

업 & 다운

by eungSe__ 2024. 5. 9.
package game;

import java.util.Scanner;

//업 & 다운 게임~
//추가 옵션 : 기회는 총 3번- 3번이상 틀릴경우 "컴퓨터 승리~"
public class updown {
	public static void main(String[] args) {
		new ud().gm();
	}
}

class ud{
	int pc,user;
	int count = 0; //게임 실패 횟수
	Scanner sc = null;

	public void gm() {
		/* random 사용법 
		//(int) : 강제로 타입 바꿔줌
		int a = (int)Math.ceil(Math.random()*10); //올림
		int b = (int)Math.floor(Math.random()*10); //절삭
		int c = (int)Math.round(Math.random()*10); //반올림
		*/
		
		this.pc = (int)Math.ceil(Math.random()*10);
		this.sc = new Scanner(System.in);
		do {
			if(count < 3) {
				System.out.println("1~20까지의 숫자 하나를 입력하세요 : ");
				this.user = this.sc.nextInt(); //사용자가 입력하는 숫자값
				if(this.pc == this.user) {
					System.out.println("정답입니다~ : " + this.user);
					break;
				}else {
					if(this.pc < this.user) {
						System.out.println("DOWN.");
					}else {
						System.out.println("UP.");
					}
					count++;
				}	
			}else {
				System.out.println("^___^ 컴퓨터 승리~~!!!!!!!!!!!!!!!!!!");
				break;
			}
		}while(true); //무한반복문
		
		
		
	}
}

'MEMO > Java-memo' 카테고리의 다른 글

object 배열  (0) 2024.05.14
문자열 배열 <-> 정수 배열 변환 방법  (0) 2024.05.11
getter & setter  (0) 2024.05.08
IDE install  (0) 2024.05.02
This  (0) 2024.04.26