已总结到 通过编辑统计数量的几种思路
1、统计符合条件的
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) - c == 0) {
count++;
}
}
2、遇到不符合条件的就终止统计
终止条件 + 终止处理(跳出循环) + 抽取重复操作(符合条件 总数+1) 这个就是递归的思想
int total = 0; // 总数记录器
for (int i = str.length() - 1; i >= 0; i--) { // 遍历从最后一项到第一项
if (str.charAt(i) == ' ') { // 终止条件
break; // 跳出循环
}
total++; // 符合条件 总数+1
}
3、遇到不符合条件的就重新计数
int length = 0;
while ('\n' != (c = (char)inputStream.read())) {
length++;
if (c == ' ') {
length = 0;
}
}
例子:8位一截取
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int loop = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if (loop == 8) { // 遇到不符合条件的就重新计数
System.out.println(sb);
sb = new StringBuilder();
loop = 0;
}
if (str.charAt(i) != ' ') { // 统计符合条件的
sb.append(str.charAt(i));
loop++;
}
}
if (sb.length() < 8) {
for (int i = sb.length(); i < 8; i++) {
sb.append(0);
}
}
System.out.println(sb);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
while ((str = br.readLine()) != null) {
int len = str.length();
int start = 0;
while (len >= 8) {
System.out.println(str.substring(start, start + 8));
start += 8;
len -= 8;
}
if (len > 0) {
char[] tmp = new char[8];
for (int i = 0; i < 8; i++) {
tmp[i] = '0';
}
for (int i = 0; start < str.length(); i++) {
tmp[i] = str.charAt(start++);
}
System.out.println(String.valueOf(tmp));
}
}