java程序问题:输入一个字符串,再输入要查找的字符,判断该字符在该字符传中传中出现了几次
1个回答
展开全部
import java.util.Scanner; class Searchable { public static void search(String input, String search) { String source = input; int count = 0; int index = -1; while ((index = input.indexOf(search)) != -1) { count++; input = input.substring(index + search.length()); } System.out.println("[" + search + "]" + "在[" + source + "]中出现了[" + count + "]次。"); } } public class Search { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); print("请输入一个字符串:"); String input = scanner.next(); print("请输入要查找的字符:"); String search = scanner.next(); Searchable.search(input, search); scanner.close(); } private static void print(Object o) { System.out.print(o); } }
记得采纳啊
记得采纳啊
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询