私は、文字列で、ユーザ入力されたキーワードの平均開始位置を見つけようとしています。私はindexOf()を使ってみましたが、正しく初期化できないようです。誰か助けてくれますか?
私が探している出力例は次のようになります。平均的な開始位置はwhile
ループ内の2つのif
sが終わり;
を持っている4.5
import java.util.Scanner;
public class Lab6Loops {
public static void main(String[] args) {
String keywordString;
String inputString;
Scanner keyboard = new Scanner (System.in);
int numofSentences = 0;
int numofKeyword = 0;
System.out.println ("Enter a keyword. We will search each sentence for this word.");
keywordString = keyboard.nextLine();
System.out.println ("Please enter a sentence or type 'stop' to finish");
inputString = keyboard.nextLine();
while(!inputString.equals ("stop"))
{
if(inputString.contains (inputString));
numofSentences = numofSentences + 1;
if(inputString.contains (keywordString));
numofKeyword = numofKeyword + 1;
System.out.println ("Enter a line of text or 'stop' to finish");
inputString = keyboard.nextLine();
}
System.out.println ("You entered " + numofSentences + " sentences");
System.out.println ("You have " + numofKeyword + "sentences that contain the keyword");
}
}
あなたが実際に取得する出力は何ですか?デバッガであなたのプログラムを踏んだときに何を学びましたか? –
私はしました averagePosition = indexOf(keywordString); しかし、それは動作しませんでした。 numofSentencesとnumofKeywordで行ったように変数を初期化する必要がありますか? – user1276514
私は文字列内の単語をindexOfにする方法を知っていますが、問題は私がユーザー入力キーワードを行う方法を知らないということです。 私は例えばindexOf( "word")を使うことができます。 – user1276514