Fork me on Gitee

有趣的小题目

有趣的小题目#

public class file {
/*检测文件中a,b所在的行数和所在的位置,
 * 例如:
 * 字符串“ffgtahtibjkli”输出
 * 1
 * 5
 * 10
 * 
 * 注意:
 * 1.要检测出所在的行数
 * 2.考虑aabb这种情况
 * 3.考虑出现多个a但是不连续的情况
 * */
public static void main(String[] args) {
	int count1=0;
	int count2=0;
	int q = 0;
	int[] t = null;
	String str2=null;
	String str1 = "asdfghjkal\nzxcvbnm\nasdfaabbnm";
	String[] len = str1.split("\n");
	//System.out.println(len.length);
	for(int i=0;i<len.length;i++) {
		str2 = len[i];
		System.out.println("这是第"+(i+1)+"行");
		for(int j = 0; j < str2.length(); j++){
			if(str2.charAt(j)=='a'){
			count1++;
			System.out.println(j+1);
				}
			if(str2.charAt(j)=='b'){
			count2++;
			System.out.println(j+1);
				}
			}
	}
	
	
	System.out.println("a共有"+count1+"个");
	System.out.println("b共有"+count2+"个");
	//System.out.println("Hello world!");
	}
}
posted @ 2018-07-21 09:49  明叶师兄。  阅读(148)  评论(0编辑  收藏  举报