发布网友 发布时间:2024-10-02 20:07
共2个回答
热心网友 时间:2024-10-06 23:36
package com;
import java.io.*;
public class Test
{
public static void main(String[] args) throws Exception
{
String str = "abcaaabckljldabcljljf";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入要查找的单词");
String findStr = br.readLine();
int strIndex = 0;
int sum = 0;
if (findStr != null && findStr.length() != 0)
{
strIndex = str.indexOf(findStr);
if (strIndex == -1)
{
System.out.println("没找到");
}
else
{
System.out.print("位置:");
while (strIndex != -1)
{
System.out.print(strIndex + " ");
//从之前找的位置的后面接着找
strIndex = str.indexOf(findStr, strIndex + 1);
sum++;
}
System.out.println("\r\n总共:" + sum + "个");
}
}
}
}
热心网友 时间:2024-10-06 23:35
package test;
public class Practice
{
private static void readWord ( String input, String word, int offset, int count )
{
offset = input.indexOf (word, offset);
if (offset != -1)
{
System.out.println (word + " 在第 " + offset + " 个位置出现过.");
readWord (input, word, ++offset, ++count);
}
else
{
System.out.println (word + " 总共出现了:" + count + " 次.");
}
}
public static void main ( String[] args )
{
String input = "Look buddy, U got work hard and put yourself to the java, once U learned the heart of java, I can guarantee that U win.";
String word = "java";
readWord (input, word, 0, 0);
}
}