Java写法键盘录入一个大字符串,再录入一个小字符串 2.统计小字符串在大字符串中出现的次数 3.代码运行打印格式如下: 请输入大字符串:woaiheima,heimabutongyubaima,wulunheimahaishibaima,zhaodaogongzuojiushihaoma 请输入小字符串:heima 控制台输出:共出现3次
时间: 2023-06-14 19:02:32 浏览: 83
以下是Java代码实现:
```java
import java.util.Scanner;
public class StringCount {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入大字符串:");
String bigStr = scanner.nextLine();
System.out.print("请输入小字符串:");
String smallStr = scanner.nextLine();
int count = 0;
int index = bigStr.indexOf(smallStr);
while (index != -1) {
count++;
index = bigStr.indexOf(smallStr, index + 1);
}
System.out.println("共出现" + count + "次");
}
}
```
运行结果:
```
请输入大字符串:woaiheima,heimabutongyubaima,wulunheimahaishibaima,zhaodaogongzuojiushihaoma
请输入小字符串:heima
共出现3次
```
阅读全文