L1-070 吃火锅 - java

题目解析

输出一共有多少个信息

如果 信息有 chi1 huo3 guo1 那么输出 第一个 chi1 huo3 guo1 出现再第几行 和 共有多少个 chi1 huo3 guo1
否则 输出 -_-#

解题思路

indexOf去找字符串是否包含即可

代码

import java.io.*;
import java.math.*;
import java.util.*;

public class Main
{

	public static void main(String[] args)
	{
		int cnt = 0, pos = 0, n = 0;
		while (sc.hasNext())
		{
			String str = sc.nextLine();
			if (str.equals("."))
				break;

			n++;
			if (str.indexOf("chi1 huo3 guo1") != -1)
			{
				if (cnt == 0)
					pos = n;
				cnt++;
			}
		}
		out.println(n);
		out.println(cnt == 0 ? "-_-#" : pos + " " + cnt);

		out.flush();
		out.close();
	}

	static Scanner sc = new Scanner(System.in);
	static PrintWriter out = new PrintWriter(System.out);
}


indexOf


团体程序设计天梯赛-练习集-java

赞赏