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

arraylist와 강화for문을 잘 이해를 못해서 다음주 화요일까지 코드를 다시 짜와도 될까요? #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

leegunwoooo
Copy link

No description provided.

@cabbage16
Copy link
Member

너무 수고하셨습니다!
대체적으로 문제 이해를 잘못 이해하신 것 같아서 그에 대한 리뷰를 달아드렸습니다.
코드의 원리나 작동 방식등을 이해하고 코드를 작성하시면 훨씬 더 양질의 코드가 나올거에요. 너무 수고많으셨고 리뷰를 읽어보시고 수정해주시길 바라겠습니다 :)

@cabbage16 cabbage16 self-requested a review July 12, 2024 14:31
Comment on lines +20 to +21
String balls = n.nextLine();
ball = balls.split(",");
Copy link
Member

Choose a reason for hiding this comment

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

String으로 받고 split() 을 사용할 필요 없이
ball = n.nextLine().split(",")
과 같이 바로 문자열 배열에 입력받을 수 있습니다.

Comment on lines +42 to +50
Attacker[] attackerArray = {new Attacker("attacker1"), new Attacker("attacker2")};
int flag = 0;
ArrayList<Attacker> attackers = Arrays.stream(attackerArray)
.collect(Collectors.toCollection(ArrayList::new));

Goalkeeper[] goalkeeperArray = {new Goalkeeper("goalkeeper")};

ArrayList<Goalkeeper> goalkeepers = Arrays.stream(goalkeeperArray)
.collect(Collectors.toCollection(ArrayList::new));
Copy link
Member

Choose a reason for hiding this comment

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

attacker와 goalkeeper를 클래스로 선언한 후, 객체 배열을 만드시고 그 객체배열을 다시 ArrayList로 바꾸셨는데,
ArrayList<String> attacker1;
이런식으로 클래스가 아닌 공의 값을 담는 ArrayList로 그냥 각각 선언하시면 됩니다.

Comment on lines +60 to +71
for(int i = 0; i < 5; i++) {
flag=0;
for(int j=0; j<4; j++){
if(attackerArray[0].ball[i].equals(goalkeeperArray[0].ball[j])){
flag = 1;
break;
}
}
if(flag==1){
attackerArray[0].count--;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

flag 변수가 존재하는 이유를 모르겠네요. 골키퍼와 공이 겹치면 flag를 바꿀 필요 없이 그냥 바로 공격수의 점수를 차감하는게 훨씬 간결합니다.

Comment on lines +74 to +75
for(int j=0; j<4; j++){
if(attackerArray[1].ball[i].equals(goalkeeperArray[0].ball[j])){
Copy link
Member

Choose a reason for hiding this comment

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

이중 for문을 사용할 필요가 없습니다.
ArrayList.contains() 메서드에 대해 알아보세요.

attackerArray[0].count--;
}
}
for(int i = 0; i < 5; i++) {
Copy link
Member

Choose a reason for hiding this comment

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

공격수의 점수를 계산하는 곳에도
for(String i : attackerArray[1].ball)
이런식으로 for each문법을 사용하면 훨씬 더 간결해집니다.

Copy link
Member

Choose a reason for hiding this comment

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

골키퍼 공 목록을 ArrayList가 아닌 배열로 선언해버려서 stream.toList()를 사용해야겠네요.

import java.util.stream.Collectors;

class Attacker {
private static Scanner n = new Scanner(System.in);
Copy link
Member

Choose a reason for hiding this comment

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

Java에서 외부 리소스를 이용하는 경우, 항상 리소스를 모두 사용한 이후에는 close()를 해주는 것이 중요합니다.
객체 안에 private로 선언한 경우에는 따로 닫는 메서드를 만들어서 close 해주어야 합니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants