R7-20 java-jmu-m02-寻找包含密码的字符串 (25 分)
输入密码x与次数n。然后在若干行字符串中(以end为结尾)寻找包含指定密码(x)的字符串的。
一旦找到就输出该字符串所在行数及该行字符串 。 最后输出包含密码x的字符串行数。
在寻找过程中,如果找到n次包含密码x的字符串则直接跳出循环,否则一直查找直到碰到end为止。
输入格式:
输入密码x
输入次数n
输入若干字符串以end结束
输出格式:
该字符串所在行数及包含密码x的字符串
符合条件的字符串出现的次数
输入样例1:
king 3 The king is coming! Where is he now? He's in the kingdom. Are you sure? !!!kingnevertelllie!!! king is the king end
输出样例1:
1 The king is coming! 3 He's in the kingdom. 5 !!!kingnevertelllie!!! 3
输入样例2
java 3 Do you like coffe? Yes, i like java. java is also my favorite King don't love coffe. king is the king end
输出样例2:
1. 2 Yes, i like java. 2. 3 java is also my favorite 3. 2
R7-20 java-jmu-m02-寻找包含密码的字符串 (25 分) 输入密码x与次数n。然后在若干行字符串中(以end为结尾)寻找包含指定密码(x)的字符串的。 一旦找到就输出该字符串所在行数及该行字符串 。 最后输出包含密码x的字符串行数。 在寻找过程中,如果找到n次包含密码x的字符串则直接跳出循环,否则一直查找直到碰到end为止。 输入格式: 输入密码x 输入次数n 输入若干字符串以end结束 输出格式: 该字符串所在行数及包含密码x的字符串 符合条件的字符串出现的次数 输入样例1: king 3 The king is coming! Where is he now? He's in the kingdom. Are you sure? !!!kingnevertelllie!!! king is the king end 输出样例1: 1 The king is coming! 3 He's in the kingdom. 5 !!!kingnevertelllie!!! 3 输入样例2: java 3 Do you like coffe? Yes, i like java. java is also my favorite King don't love coffe. king is the king end 输出样例2: 2 Yes, i like java. 3 java is also my favorite 2 import java.util.Scanner; public class J_20 { public static void main(String[] args) { @SuppressWarnings("resource") Scanner sc=new Scanner(System.in); String x=sc.nextLine(); int n=sc.nextInt(); String str; int hang=-1,count=0,i=-1; str=sc.nextLine(); while(sc.hasNext()) { if(str=="end") { break; } hang++; i = str.indexOf(x); if(i!=-1) { System.out.println(hang+" "+str); count++; if(count==n) { break; } } str=sc.nextLine(); } System.out.println(count); } }