public class SubstringCount
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a word longer than 4 characters, and press q to quit");
int count = 0;
while (scan.hasNextLine())
{
System.out.println("Enter a word longer than 4 characters, and press q to quit");
String word = scan.next();
if (word.substring(0,4).equals("Stir"))
{
count++;
System.out.println("Enter a word longer than 4 characters, and press q to quit");
scan.next();
}
else if (word.equals("q"))
{
System.out.println("You have " + count + ("words with 'Stir' in them"));
}
else if (!word.substring(0,4).equals("Stir"))
{
System.out.println("Enter a word longer than 4 characters, and press q to quit");
scan.next();
}
}
}
}
ここでは、ユーザーが入力した単語の数に部分文字列「Stir」が含まれていることを出力する必要があります。ただし、これを機能させる方法がわかりません。または、そもそもこれを正しく実行したかどうかもわかりません。
助けてくれてありがとう!