JAVA/java
[문제풀이] " 배열 없이" 야구게임 만들기
빵으니
2020. 2. 25. 18:06
아직 배열을 배우기 전이기 때문에 반복문을 사용해서 야구게임을 만들어 봤습니다!
▶게임 설명
1~9 숫자 중 3가지 숫자 입력 (중복 불가)
1. 숫자와 자리가 모두 일치할 경우 "Strike"
2. 자리는 일치하지 않지만 숫자는 일치할 경우 "Ball"
3. 숫자와 자리 모두 일치하지 않을 경우 "Out"
▷point
computer 값 : 랜덤 값 받아오기
user 값 : 스캐너로 입력 받기
중복 제거 & 반복
조건 설정 & 반복
▶소스코드
package chap03;
import java.util.Scanner;
public class FlowTest27 {
public static void main(String[] args) {
int com1 = (int)(Math.random()*9)+1;
int com2 = (int)(Math.random()*9)+1;
while(com1==com2) {
com2 = (int)(Math.random()*9)+1;
}
int com3 = (int)(Math.random()*9)+1;
while(com1==com3||com2==com3) {
com3 = (int)(Math.random()*9)+1;
}
//System.out.println(com1 + " " + com2 + " " + com3);
Scanner sc = new Scanner(System.in);
int num1 = 0;
int num2 = 0;
int num3 = 0;
int strike = 0;
int ball = 0;
int out = 0;
while(strike != 3) {
System.out.println("user> 숫자 세 개를 입력하세요.");
strike = 0; //돌려줄 때마다 매번 초기화 필요
ball = 0;
out = 0;
num1 = sc.nextInt();
num2 = sc.nextInt();
num3 = sc.nextInt();
if(com1 == num1) {
strike += 1;
}else if(com1 == num2 || com1 == num3){
ball += 1;
}
else {
out+=1;
}
if(com2 == num2) {
strike += 1;
}else if(com2 == num1 || com2 == num3) {
ball += 1;
}
else {
out+=1;
}
if(com3 == num3) {
strike += 1;
}else if(com3 == num1 || com3 == num2) {
ball += 1;
}
else {
out+=1;
}
System.out.println("Strike : " + strike + ", Ball : " + ball + ", Out : " + out);
}
}
}
▶소스코드 (선생님ver.)
package chap03;
import java.util.Scanner;
public class Baseball {
public static void main(String[] args) {
//컴퓨터값 랜덤
//랜덤 값은 0~0.9999999999*9+1
int com1 = 0; //while문에서 선언하면 지역변수가 되므로 미리 선언
int com2 = 0;
int com3 = 0;
//중복이 될 경우 랜덤 값 뽑기 무한반복
while(true) {
com1 = (int)(Math.random()*9)+1; //랜덤으로 받은 값, int로 변환
com2 = (int)(Math.random()*9)+1;
com3 = (int)(Math.random()*9)+1;
//중복값 방지
if(!(com1 == com2 || com1 == com3 || com2 == com3)) {
break;
}
}
//사용자 값 스캐너 입력
int user1 = 0;
int user2 = 0;
int user3 = 0;
//S,B,O 변수 설정
int strike = 0, ball = 0, out = 0;
//System.out.printf("%s%s%s%n", com1,com2,com3);
//스캐너는 한번만 돌 수 있도록 반복문 밖에서 설정
Scanner sc = new Scanner(System.in);
//while문 무한루프
while(true) {
strike = 0; //새 게임 할 때마다 값을 초기화 (값이 누적되지 않도록)
ball = 0;
out = 0;
System.out.println("첫번째 숫자를 넣으세요(1~9).>");
user1 = sc.nextInt(); //바깥에서 초기화 하고 안에서 변수값 설정
System.out.println("두번째 숫자를 넣으세요(1~9).>");
user2 = sc.nextInt();
System.out.println("세번째 숫자를 넣으세요(1~9).>");
user3 = sc.nextInt();
if(com1 == user1) {
strike++;
}else if(com1 == user2) {
ball++;
}else if(com1 == user3) {
ball++;
}
if(com2 == user2) {
strike++;
}else if(com2 == user1) {
ball++;
}else if(com2 == user3) {
ball++;
}
if(com3 == user3) {
strike++;
}else if(com3 == user1) {
ball++;
}else if(com3 == user2) {
ball++;
}
out = 3 - (strike + ball);
System.out.printf("%sS %sB %sO %n", strike,ball,out);
if(strike == 3) {
break; //빠져나가기 전에 찍어라!
}
}
System.out.println("빙고!!");
}
}
▶소스코드 (메소드 사용ver.)
package chap03;
import java.util.Scanner;
public class Baseball {
public static void main(String[] args) {
//컴퓨터값 랜덤
//랜덤 값은 0~0.9999999999*9+1
int com1 = 0; //while문에서 선언하면 지역변수가 되므로 미리 선언
int com2 = 0;
int com3 = 0;
boolean check = false; //비중복
//중복이 될 경우 랜덤 값 뽑기 무한반복
while(true) {
com1 = (int)(Math.random()*9)+1; //랜덤으로 받은 값, int로 변환
com2 = (int)(Math.random()*9)+1;
com3 = (int)(Math.random()*9)+1;
check = isCheck(com1, com2, com3);
if(!check) {
break;
}
}
//사용자 값 스캐너 입력
int user1 = 0;
int user2 = 0;
int user3 = 0;
//S,B,O 변수 설정
int strike = 0, ball = 0, out = 0;
//System.out.printf("%s%s%s%n", com1,com2,com3);
//스캐너는 한번만 돌 수 있도록 반복문 밖에서 설정
Scanner sc = new Scanner(System.in);
//while문 무한루프
while(true) {
strike = 0; //새 게임 할 때마다 값을 초기화 (값이 누적되지 않도록)
ball = 0;
out = 0;
System.out.println("첫번째 숫자를 넣으세요(1~9).>");
user1 = sc.nextInt(); //바깥에서 초기화 하고 안에서 변수값 설정
System.out.println("두번째 숫자를 넣으세요(1~9).>");
user2 = sc.nextInt();
System.out.println("세번째 숫자를 넣으세요(1~9).>");
user3 = sc.nextInt();
check = isCheck(user1, user2, user3);
if(!check) {
System.out.println("중복값을 입력하지 마세요. 다시 입력>");
continue;
}
if(com1 == user1) {
strike++;
}else if(com1 == user2) {
ball++;
}else if(com1 == user3) {
ball++;
}
if(com2 == user2) {
strike++;
}else if(com2 == user1) {
ball++;
}else if(com2 == user3) {
ball++;
}
if(com3 == user3) {
strike++;
}else if(com3 == user1) {
ball++;
}else if(com3 == user2) {
ball++;
}
out = 3 - (strike + ball);
System.out.printf("%sS %sB %sO %n", strike,ball,out);
if(strike == 3) {
break; //빠져나가기 전에 찍어라!
}
}
System.out.println("빙고!!");
}
//중복값 처리 메서드
//false값이 리턴되면 중복이 안 된 것. ture값이 리턴되면 중복된 것.
static boolean isCheck(int n1, int n2, int n3) {
boolean check = false; //리턴 할 변수 선언
if(!(n1 == n2 || n1 == n3 || n2 == n3)) {
check = true;
}
return check; // 위에 있는 변수를 리턴
}
}