对不起,我对我的程序非常困惑,我试图调用一个方法,该方法返回一个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;
}
}
你能告诉我们你有什么错误吗?顺便说一下,为什么不使用'equals'而不是检查所有字符是否相等?... – Maroun
对不起,我正在收到错误:找不到符号 \t \t checkMatchesSomewhere(secretword,guessword); //在secretWord中 \t \t^ 符号的地方检查匹配数量:可变secretWord中 位置:类应用 对我所说的方法CheckMatchesSomewhere(...) –
我会强烈建议在开始一个良好的入门书行Java或Oracle的教程。这里有很多问题都表明你对绝对基本知识并没有一个牢固的把握。 –