자바 return에 대해서 궁금합니다.

조회수 4113회
public class SimpleDotComTestDrive {

    public static void main(String[] args) {
        SimpleDotCom dot = new SimpleDotCom();
        int[] locations = {2,3,4};
        dot.setLocationCells(locations);
        String userGuess = "2";
        String result =  dot.checkYourself(userGuess);//==result==hit
        userGuess = "3";
        result =  dot.checkYourself(userGuess);
        userGuess = "4";
        result =  dot.checkYourself(userGuess);


    }
}
class SimpleDotCom{

    int [] locationCells;
    int numOfHits = 0;
    public void setLocationCells (int[] locs) {

        locationCells = locs; // {2,3,4}
    }

    public String checkYourself(String stringGuess) {
        int guess = Integer.parseInt(stringGuess);
        String result = "miss";
        for(int cell : locationCells) {
            if(guess== cell) {
                result = "hit";
                numOfHits++;
                System.out.println(numOfHits);
                break;
                }
            }

            System.out.println(result);
            if (numOfHits == locationCells.length) {
                result = "kill";
                System.out.println(result);

            }

            return result;
        }



}

위의 코드에서 return result;

의 의미를 잘 모르겠습니다.

SimpleDotComTestDrive안에 있는 result의 값은

SimpleDotComTestDrive 클래스는 void 값을 갖기 때문에 return값이 없다고 배웠는데

return result;의 이유가 정말 궁금합니다.

3 답변

  • 일단 미완성된 코드같네요. 그리고 SimpleDotCom의 checkYourself 메소드가 return하는건데 SimpleDotComTestDrive에 return값이 없는건 상관이 없을 것 같습니다.

  • return result;

    메서드(method)의 실행 결과를 되돌려주는데, 그 결과 값은 result 에 담겨있다는 뜻입니다.

    즉, SimpleDotCom 클래스에 있는 checkYourself 메서드의 실행 결과 값이며 여기서 호출했네요. String result = dot.checkYourself(userGuess);//==result==hit

    정확히 말씀드리자면 SimpleDotComTestDrive 클래스가 void 값을 갖는게 아니라, SimpleDotComTestDrive 클래스에 리턴타입이 void 인 main 메서드가 정의되어 있는거죠.

  •  1. public class SimpleDotComTestDrive {
     2. 
     3.    public static void main(String[] args) {
     4.        SimpleDotCom dot = new SimpleDotCom();
     5.        int[] locations = {2,3,4};
     6.        dot.setLocationCells(locations);
     7.        String userGuess = "2";
     8.        String result =  dot.checkYourself(userGuess);//==result==hit
     9.        userGuess = "3";
     10.      result =  dot.checkYourself(userGuess);
     11.      userGuess = "4";
     12.      result =  dot.checkYourself(userGuess);
     13.   }
     14. }
    
     class SimpleDotCom{
    
                 int [] locationCells;
                 int numOfHits = 0;
                 public void setLocationCells (int[] locs) {
    
                     locationCells = locs; // {2,3,4}
                 }
    
                 public String checkYourself(String stringGuess) {
                     int guess = Integer.parseInt(stringGuess);
                     String result = "miss";
                     for(int cell : locationCells) {
                         if(guess== cell) {
                             result = "hit";
                             numOfHits++;
                             System.out.println(numOfHits);
                             break;
                             }
                         }
    
                         System.out.println(result);
                         if (numOfHits == locationCells.length) {
                             result = "kill";
                             System.out.println(result);
    
                         }
    
                         return result;
                     }
    
    
    
             }
    

    위 코드를 보시고 처음부터 따라가시면

    세번째 줄

    SimpleDotCom dot = new SimpleDotCom();
    

    에서 SimpleDotCom Class를 New 로 생성합니다.

    그리고 궁금하신 return result는

    8.번째 줄에서

    String result =  dot.checkYourself(userGuess);//==result==hit
    

    dot이라고 위에서 생성한 클래스의 checkYourself메소드를 호출합니다.

    checkYourself메소드를 보시면

     public String checkYourself(String stringGuess) {
                     int guess = Integer.parseInt(stringGuess);
                     String result = "miss";
                     for(int cell : locationCells) {
                         if(guess== cell) {
                             result = "hit";
                             numOfHits++;
                             System.out.println(numOfHits);
                             break;
                             }
                         }
    
                         System.out.println(result);
                         if (numOfHits == locationCells.length) {
                             result = "kill";
                             System.out.println(result);
    
                         }
    
                         return result;
                     }
    

    String으로 return을 정의 한게 보이실겁니다.

    그리하여 checkYourself에서

    return result;

    즉 String 형의 result변수를 SimpleDotComTestDrive 클래스안에있는 메인 메소드 result변수로 리턴해주는겁니다.

    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)