有这样一句话“I have a dog,I love dog,dog is my friend”,请用java编写一个方

有这样一句话“I have a dog,I love dog,dog is my friend”,请用java编写一个方法,计算上面这句话包含“dog”的个数.
zsz0579 1年前 已收到1个回答 举报

eaxa 幼苗

共回答了9个问题采纳率:88.9% 举报

咱们就用最简单最直观的吧
public static void main(String[] args) {
x05x05String content = "I have a dog,I love dog,dog is my friend";
x05x05getDogNums(content);
x05x05System.out.println(mNums);
x05}
x05
x05static int mNums;
x05public static void getDogNums(String str) {
x05x05int index = str.indexOf("dog");
x05x05if (index != -1) {
x05x05x05mNums++;
x05x05x05getDogNums(str.substring(index+1));
x05x05}
x05}

1年前

9
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 0.063 s. - webmaster@yulucn.com