抱歉,我对我的程序感到非常困惑,在该程序中,我试图调用一个返回 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;
}
}