Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[숫자 야구 게임] 박수지 미션 제출합니다. #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 79 additions & 1 deletion src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,85 @@
package baseball;

import java.util.Scanner;
import java.util.Random;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
{
Scanner input = new Scanner(System.in);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제공한 Console.readline()을 활용해주세요

Random rand = new Random();
int ranNum = rand.nextInt(900)+100;
String sNum = String.valueOf(ranNum);
String[] arrStr = sNum.split("");

int strikeNum = 0;
int ballNum = 0;
System.out.println("숫자 야구 게임을 시작합니다.");
while(true){
int in;
System.out.print("숫자를 입력해주세요 : ");
in = input.nextInt();
if(in >=1000 || in <= 100){
throw new IllegalArgumentException("세자리 수를 입력해야 합니다.\n프로그램을 종료합니다.");
}
int[] arr = new int[3];
arr[0] = in/100;
arr[1] = (in%100)/10;
arr[2] = in%10;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String str;
str.charAt()함수를 활용하는 방법을 생각해보세요

String[] arrStr2 = {arr[0]+"",arr[1]+"",arr[2]+""};

for(int i = 0; i < 3; i++){
if(arrStr2[i].equals(arrStr[i])){
strikeNum++;
}
}
if(strikeNum == 2){
System.out.print("2스트라이크\n");
strikeNum=0;
}else if(strikeNum == 1){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(arrStr2[j].equals(arrStr[i])){
ballNum++;
}
}
}
if(ballNum-strikeNum != 0)
System.out.print(ballNum-strikeNum + "볼 ");
System.out.print("1스트라이크\n");
ballNum=0;
strikeNum=0;
}else if(strikeNum == 0){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(arrStr2[j].equals(arrStr[i])){
ballNum++;
}
}
}
if(ballNum-strikeNum == 0)
System.out.print("낫싱\n");
else{
System.out.print(ballNum-strikeNum + "볼 \n");
strikeNum=0;
ballNum=0;
}
}else if(strikeNum == 3) {
System.out.print("3스트라이크 \n3개의 숫자를 모두 맞히셨습니다! 게임 종료\n");
strikeNum=0;
ballNum=0;
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
int re = input.nextInt();
if (re == 2)
break;
else{
ranNum = rand.nextInt(900)+100;
sNum = String.valueOf(ranNum);
arrStr = sNum.split("");
continue;
}
}
}
}
}
}