-
Notifications
You must be signed in to change notification settings - Fork 3
Mission 1 - 클래스분리 #6
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
base: gjwlgh
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,70 +2,34 @@ | |
| import java.util.Scanner; | ||
|
|
||
| public class Application { | ||
| public static void setComputerNumbers(int computer[], Random random){ | ||
| int i =0; | ||
| while(i<3){ | ||
| int x=0; | ||
| if(i==0){ | ||
| computer[i]=random.nextInt(8)+1; | ||
| } | ||
| else{ | ||
| computer[i]=random.nextInt(8)+1; | ||
| if(i==1 && computer[i]==computer[i-1]) continue; | ||
| if(i==2 && computer[i]==computer[i-1] | computer[i]==computer[i-2]) continue; | ||
| } | ||
| i++; | ||
| } | ||
| } | ||
| public static int checkUserAndComputer(int user[],int computer[], int tmp, int ball, int strike){ | ||
| user[0]=tmp/100; | ||
| user[1]=(tmp/10)%10; | ||
| user[2]=tmp%10; | ||
|
|
||
| for (int j = 0; j < 3; j++) { | ||
| for (int k = 0; k < 3; k++) { | ||
| if (computer[j] == user[k]) { | ||
| if (j == k) strike++; | ||
| else ball++; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| if (strike == 0 && ball == 0) | ||
| System.out.printf("낫싱"); | ||
| else if (ball == 0) | ||
| System.out.printf(strike + "스트라이크 "); | ||
| else if (strike == 0) | ||
| System.out.printf(ball + "볼 "); | ||
| else | ||
| System.out.printf(strike + "스트라이크 " + ball + "볼"); | ||
| return strike; | ||
| } | ||
| public static void main(String[] args) { | ||
| final int INITIAL_COUNT = 0; | ||
| final int Initial_Num = 0; | ||
| final int MAX_NUMBER_LENGTH = 3; | ||
| int game = 1; | ||
|
|
||
| while(game==1){ | ||
| int[] computer; | ||
|
Member
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. Collection의 List를 사용해보는건 어떨까요? |
||
| int[] user; | ||
| int strike = Initial_Num; | ||
|
|
||
| int computer[] = new int[3]; | ||
| int user[] = new int[3]; | ||
| int strike = INITIAL_COUNT; | ||
| int ball=INITIAL_COUNT; | ||
| Random random = new Random(); | ||
| Scanner input = new Scanner(System.in); | ||
|
|
||
| setComputerNumbers(computer,random); | ||
| computer = Computer.Create(); | ||
|
|
||
| while(strike<MAX_NUMBER_LENGTH) { | ||
| int strikeAndBallArry[] = new int[2]; | ||
|
|
||
| while(strike<3) { | ||
| System.out.printf("\n숫자를 입력해 주세요 : "); | ||
| int tmp = input.nextInt(); | ||
| ball = 0; | ||
| strike = 0; | ||
|
|
||
| strike = checkUserAndComputer(user, computer, tmp, ball, strike); | ||
| user = User.Create(); | ||
| strikeAndBallArry = Computer.Check(user,computer); | ||
| strike = Computer.numBallAndStrike(strikeAndBallArry); | ||
| } | ||
|
|
||
| System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임종료\n"); | ||
| System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); | ||
|
|
||
| if(input.nextInt()== 2) { | ||
| game = 2; | ||
| } else { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import java.util.Random; | ||
|
|
||
| public class Computer { | ||
|
|
||
| public static int[] Create(){ | ||
|
Member
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. 무엇을 만들어 내는지 표현할수 있지 않을까요? Computer.create()는 마치 생성자와 같은 느낌이 들어 혼동이 올 수 있을것 같아요! |
||
| Random random = new Random(); | ||
| int numBoundary =0; | ||
| int[] _computerNum = new int[3]; | ||
|
|
||
| while(numBoundary<3){ | ||
| if(numBoundary==0){ | ||
| _computerNum[numBoundary]=random.nextInt(8)+1; | ||
|
Member
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. 자바 컨벤션에 따르면 변수명에 언더바를 붙이지 않습니다 :) |
||
| } | ||
| else{ | ||
| _computerNum[numBoundary]=random.nextInt(8)+1; | ||
| if(numBoundary==1 && _computerNum[numBoundary]==_computerNum[numBoundary-1]) continue; | ||
|
Member
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. 한 줄로 끝나더라도 중괄호는 열어주세요! |
||
| if(numBoundary==2 && _computerNum[numBoundary]==_computerNum[numBoundary-1] | _computerNum[numBoundary]==_computerNum[numBoundary-2]) continue; | ||
| } | ||
| numBoundary++; | ||
| } | ||
| return _computerNum; | ||
| } | ||
|
|
||
| public static int[] Check(int[] _userNum, int[] _computerNum){ | ||
|
Member
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. 메소드명은 소문자로 시작하고, 클래스명은 대문자로 시작합니다. |
||
| int _strike =0; | ||
| int _ball = 0; | ||
| int[] _strikeAndBallArry = new int[2]; | ||
|
|
||
| for (int j = 0; j < 3; j++) { | ||
| for (int k = 0; k < 3; k++) { | ||
| if (_computerNum[j] == _userNum[k]) { | ||
| if (j == k) { | ||
|
Member
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. indent가 4가 되는 부분이네요 😥 indent를 줄이는 가장 간단한 방법은 메소드 분리입니다! |
||
| _strike++; | ||
| } else { | ||
| _ball++; | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| _strikeAndBallArry[0]=_ball; | ||
| _strikeAndBallArry[1]=_strike; | ||
|
|
||
| return _strikeAndBallArry; | ||
| } | ||
|
|
||
| public static int numBallAndStrike(int[] _strikeAndBallArry) { | ||
| if (_strikeAndBallArry[0] == 0 && _strikeAndBallArry[1] == 0) { | ||
| System.out.printf("낫싱"); | ||
| } else if (_strikeAndBallArry[0] == 0) { | ||
| System.out.printf(_strikeAndBallArry[1] + "스트라이크 "); | ||
| } else if (_strikeAndBallArry[1] == 0) { | ||
| System.out.printf(_strikeAndBallArry[0] + "볼 "); | ||
| } else { | ||
| System.out.printf(_strikeAndBallArry[1] + "스트라이크 " + _strikeAndBallArry[0] + "볼"); | ||
| } | ||
|
|
||
| if (_strikeAndBallArry[1] == 3) { | ||
| return _strikeAndBallArry[1]; | ||
| } | ||
| return 0; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class User { | ||
|
|
||
| public static int[] Create(){ | ||
| Scanner input = new Scanner(System.in); | ||
| int[] _userNum = new int[3]; | ||
| int tmp = input.nextInt(); | ||
|
Member
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. 변수명에 의도를 담을수 있지 않을까요? |
||
|
|
||
| _userNum[0]=tmp/100; | ||
| _userNum[1]=(tmp/10)%10; | ||
| _userNum[2]=tmp%10; | ||
|
|
||
| return _userNum; | ||
| } | ||
| } | ||
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.
상수는 보통 전체 대문자와 _로 띄어쓰기를 표현한답니다 :)
밑에 작성해주신 MAX_NUMBER_LENGTH 처럼요!