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

[숫자 야구 게임] 박해원 과제 제출합니다. #14

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
87 changes: 84 additions & 3 deletions src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,88 @@
package baseball;

import java.util.Scanner;
import camp.nextstep.edu.missionutils.Console;
import camp.nextstep.edu.missionutils.Randoms;
public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
Scanner scanner= new Scanner(System.in);
boolean option = true;
while(option) {
boolean sad = true;
char[] ranArr = new char[3];
do {
int random = Randoms.pickNumberInRange(123, 987);
String randomStr = Integer.toString(random); //문자열로 바꾸기
//char 배열에 저장하기
for (int i = 0; i < 3; i++) {
ranArr[i] = randomStr.charAt(i);
}
for (int i = 0; i < 3; i++) {
System.out.print(ranArr[i]);
}
if (ranArr[0] != ranArr[1] && ranArr[1] != ranArr[2] && ranArr[0] != ranArr[2]) {
if(ranArr[1]!=0&&ranArr[2]!=0){
sad=false;
}
} else {
ranArr= new char[3];
continue;
}
}while(sad);

//주석깃허브
while (true) {
System.out.print("숫자를 입력해주세요: ");
String sscore = scanner.nextLine();
try {
if (sscore.length() != 3) {
throw new IllegalArgumentException();
}

char[] score = new char[3];
for (int i = 0; i < 3; i++) {
score[i] = sscore.charAt(i); //숫자 하나하나 따로 나누기. 비교를 위해!
}
int ball = 0, strike = 0;

for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j && score[i] == ranArr[j]) {
strike++;
} else if (i != j && score[i] == ranArr[j]) {
ball++;
}

}
}
if (strike == 3) {
System.out.println("3스트라이크\n3개의 숫자를 모두 맞히셨습니다! 게임종료");
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
int input = scanner.nextInt();
scanner.nextLine();

if (input == 2) {
System.out.println("게임종료!");
option = false;
break;
} else if (input == 1) {
break;
}

} else if (strike > 0 && ball > 0) {
System.out.println(ball + "볼 " + strike + "스트라이크");
} else if (strike > 0) {
System.out.println(strike + "스트라이크");
} else if (ball > 0) {
System.out.println(ball + "볼");
} else if (ball == 0 && strike == 0) {
System.out.println("낫싱");
}
}catch(IllegalArgumentException e){
System.out.println("잘못된 입력입니다: ");
return;
}
}

}
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/baseball/Haewon.java
Copy link
Collaborator

Choose a reason for hiding this comment

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

ㅎㅇ

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package baseball;

public class Haewon {
int a;
}