私は文字列内の単語、または基本的に単語内の単語を見つけるプログラムを作ろうとしています。私の思考プロセスは、各部分文字列が単語と一致するかどうかをチェックし、再帰を使用して一度に1文字ずつ文字列を切り取ることです。私はそれに多くの時間を費やしたが、それは動作していないと私は本当に理由を理解していない。助けてください、私はしばらくの間、これに本当に不満を抱いています。いずれかの長さが0再帰コードが正しく動作しないのはなぜですか? [閉じた!]
第二の場合
public static boolean yodo(String sen, String word)
{
String sentence = sen;
String wordy = word;
int wordLength = word.length();
int senLength = sentence.length();
boolean result = false;
if ((sentence.substring(0, wordLength)).equals(wordy))
{
result = true;
}
else
{
yodo(sentence.substring(1), wordy);
}
return result;
}
'yodo(sentence.substring(1)、wordy);'の結果は無視されます。 –
'return sentence.contains(word);' –
なぜパラメータを変更したいのでなければ変数に代入するのですか? --- 'senLength'は何のために使われますか? --- ''(sentence.substring(0、wordLength))。equals(wordy) 'は、' 'sentence.startsWith(wordy)'と同じです。 ---他の人が言ったように、あなたは再帰呼び出しの戻り値を使うべきです。 – Andreas