편집 기록

편집 기록
  • 프로필 김선우님의 편집
    날짜2017.10.17

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


    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;의 이유가 정말 궁금합니다.

  • 프로필 알 수 없는 사용자님의 편집
    날짜2017.10.16

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


    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;의 이유가 정말 궁금합니다.