ZZULIOJ 1150: Counting integers, Java
Question description
Xiao Ming's teacher gave Xiao Ming a question: count how many numbers appear in an article, please help him.
enter
Enter a string consisting of spaces, English letters, and numbers, ending with a carriage return, and the length is less than 1000.
output
Output the number of integers (not the number of numeric characters).
Sample inputCopy
365grh 27ha578
Sample outputCopy
3
hint
注意:010是两个数字,0和10
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String str = bf.readLine();
int sum = 0;
boolean ok = false;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == '0' && !ok) {
sum++;
}
if (ch > '0' && ch <= '9' && !ok) {
sum++;
ok = true;
}
if (ch == ' ' || ch > '9') {
ok = false;
}
}
bw.write(sum + "\n");
bw.close();
}
}