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();
}
}
}
}
在这里,我需要打印用户输入的单词中有多少包含子字符串“搅拌”。但是我不知道如何让它工作,或者我是否一开始就做对了!
谢谢你的帮助!