숫자맞추기 게임:7회 사용자 usr 컴퓨터 com 1~100까지의 숫자 중 하나를 입력받는다. 컴퓨터는 1~100까지의 숫자 중 하나를 랜덤하게 형성한다. user < com :up시켜 user > com : Down시켜 |
package chapter04; import java.util.*; public class Random1 { public static void main(String[] args) { Random rand = new Random(); Scanner sc = new Scanner(System.in); /* int com = (int)Math.random() * 100+1; */ int com = rand.nextInt(100)+1; //컴퓨터 랜덤 형성 //시도횟수를 7회까지만 허용하라! boolean success =false; //성공여부 int tries = 1; System.out.println("숫자맞추기 게임!도전 기회 7번"); while(true) { //무한반복 System.out.println("1~100까지의 숫자를 입력:"); int user = sc.nextInt(); if(tries >= 7) break; if(user == com) { success=true; break; //성공시 성공여부를 변경시킨다. } else if(user > com) System.out.println("Down 시켜봐!"); else if(user < com) System.out.println("up시켜봐! "); tries++; } //성공 여부에 따른 처리 if (success == true) System.out.println("축하합니다."); else System.out.println("다시 도전."); System.out.println("컴퓨터 생성값:" +com); } } |
'Coding > JAVA' 카테고리의 다른 글
[명품자바] 05장 상속 실습문제 5번/6번/7번 (0) | 2022.04.19 |
---|---|
[명품자바] 05 상속 개념/ 예제 (0) | 2022.04.19 |
JAVA_[반복문과 배열 예제 연습] (1) | 2022.03.29 |
[04] 클래스와 객체 (0) | 2022.03.29 |
java if-조건문/다중if문 예제 (0) | 2022.03.15 |