JAVA를 이용한 BaseballGame
2021. 8. 21. 08:48
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package GameGame; import java.util.Random; import java.util.Scanner; public class NewBaseballGame { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int[] game = new int[3]; int[] input = new int[3]; int tmp = 0, strike = 0, ball = 0; for (int i = 0; i < game.length; i++) { Random r = new Random(); tmp = r.nextInt(9) + 1; for (int j = 0; j < game.length; j++) { if (tmp == game[j]) { i--; } } game[i] = tmp; } tmp = 0; while (true) { System.out.println("HOW MUCH YOU TRY? : " + tmp++); for (int x = 0; x < input.length; x++) { System.out.print("TYPE YOUR " + (x + 1) + " NUMBER : "); input[x] = scan.nextInt(); } for (int y = 0; y < input.length; y++) { for (int z = 0; z < input.length; z++) { if (input[y] == game[y]) { strike++; break; } else if (input[y] == game[z] && y != z) { ball++; } } } System.out.println("strike : " + strike + " ball : " + ball + " out : " + (3 - strike - ball)); if (strike == 3) { System.out.println("CONGRATULATION!!! YOU WIN!!!"); scan.close(); break; } else { System.out.println("TRY MORE HARD!!!"); strike = 0; ball = 0; } } System.out.println("TRY COUNT : " + tmp); } } | cs |
'Computer Language > Java' 카테고리의 다른 글
Java를 이용한 Calendar Printer (0) | 2021.08.23 |
---|---|
Java를 이용한 Calendar 사용하기 (0) | 2021.08.23 |
Java를 이용한 Random Class (0) | 2021.08.20 |
Java를 이용한 Array 배열 이동 및 삭제, 내용변경 (0) | 2021.08.12 |
Java를 이용한 Multiplication Table (0) | 2021.08.04 |