forked from woowacourse-precourse/java-baseball-6
-
Notifications
You must be signed in to change notification settings - Fork 10
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
io-uty
wants to merge
1
commit into
LandvibeDev:main
Choose a base branch
from
io-uty:io-uty
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. String str; |
||
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; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
제공한 Console.readline()을 활용해주세요