2013-06-20 7 views
-2

죄송합니다. int를 반환하는 메서드를 호출하려고하지만 내 문자열 변수에 전달하려는 프로그램과 매우 혼동스러워집니다. 메소드에서 작동하는 코드가 이미 있지만, 이제 checkMatchesSomewhere()를 호출하여 자체 메소드로 이동했습니다.문자열 값을 메서드에 전달합니다.

문자열 변수 secretWord 및 secretGuess 값을 메서드에 전달하여 루프에서 사용할 수 있도록하려는 경우. 그러나 그것은 컴파일되지 않습니다. 누군가 내가 잘못하고있는 것을 말해 줄 수 있습니까? 많은 감사합니다. 나는 프로그래밍에 초보적이다.

class App 
{ 
    public static void main(String args[]) 
    { 
     App app = new App(); 
    } 
    //constructor 
    public App() 

    { 

     //variables 

     String secretWord = "berry"; 
     String guessword = "furry"; 
     secretMatches = 0; 

       //Call CheckMatchesSomewhere method 
     checkMatchesSomewhere(secretword, guessword); // checks number of matches somewhere in the secretWord 

     // print the number of times the secretChar occurs in the string word 
     System.out.println(secretMatches); 

    } 

    // METHOD THAT CHECKS FOR NUMBER OF MATCHES SOMEWHERE IN THE WORD 

     private int checkMatchesSomewhere(String secretword, String guessword) 

     { 
     // variables 
     String secretWord; 
     String guessWord; 
     int secretMatches = 0; 

     //check each letter in sequence against the secretChar 
     // 
     //a loop which reads through 'secretWord' 
     for (int j = 0; j < secretWord.length(); j++) 
     { 

      //the loop which goes through 'word' 
      for (int i = 0; i < guessWord.length(); i++) 
      { 
       if (guessWord.charAt(i) == secretWord.charAt(j)) 
       { 
        secretMatches++; 

        //break once a match is found anywhere 
        break; 
       } 

      } // end word for loop 

     } // end secretWord for loop 

     // return the number of matches somewhere 
     return secretMatches; 
     } 

} 
+2

어떤 오류가 있습니까? BTW, 모든 문자가 같은지 확인하는 대신 '평등'을 사용하는 것이 어떻습니까? ... – Maroun

+0

죄송합니다. 오류가 발생했습니다. 기호를 찾을 수 없습니다. \t \t checkMatchesSomewhere (secretword, guessword); //은 SECRETWORD \t \t^ 기호 어딘가에 일치의 수를 확인 : 변수 SECRETWORD에게 위치 : 클래스 앱 을 내가 CheckMatchesSomewhere (...) –

+0

나는 매우에 좋은 초보자의 책으로 시작하는 것이 좋습니다 것입니다 메서드를 호출 라인에 Java 또는 Oracle의 자습서 여기에는 절대적인 기초에 대해 확실하게 이해하지 못한다는 여러 가지 문제가 있습니다. –

답변

1

코드에 최소 10 개의 오류가 있습니다. 그 중 일부는 있습니다.

secretMatches = 0; //where is the datatype ?? 

그것은 자바는 대소 문자를 구분 int secretMatches = 0;

해야한다.

String secretWord = "berry"; //you declared like this 

checkMatchesSomewhere(secretword, guessword); //secretWord should pass here 

그리고 마지막으로

String secretWord = null;// you have to intialize it. 
String guessWord = null; // you have to intialize it. 

그리고 추가로 당신이없는 그들은 이미 메소드 인자이기 때문에 당신은 checkMatchesSomewhere 방법에 secretWordguessWord 변수를 선언하지 않아야 Basics of java

+0

예 .. 디버거를 사용하면 그에게 많은 도움이되었습니다. – Maroun

+1

그건 그의 문제 중 하나 * 예. –

+0

그게 전부 야. 건배. –

0

를 통해 이동하시기 바랍니다 메서드의 결과를 secretMatches에 할당하면 항상 0 개의 일치를보고합니다.

EDIT : 메소드 인수에 대소 문자가 다른 것으로 나타났습니다. 아마도 그게 의도되지 않았을 거라 생각합니다.

+0

물론 감사합니다. –

0

논리를 모르겠지만 인수가 메소드에 전달 될 때 새로운 인수를 작성하는 대신에 사용해야합니다.

checkMatchesSomewhere()에 인수를 전달하고 있지만 사용하지는 않습니다.

관련 문제