输入一串字符串(只包含数字),统计该字符串中所有偶数的个数。
public class Day1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String word = scan.next();
int flag = 0;
for(int i=0;i<word.length();i++){
if((word.charAt(i)+'0')%2==0){
flag++;
}
}
System.out.println(flag);
}
}
输出结果:
1112111211121
3
如果求奇数个数,则:
public class Day1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String word = scan.next();
int flag = 0;
for(int i=0;i<word.length();i++){
if((word.charAt(i)+'0')%2!=0){
flag++;
}
}
System.out.println(flag);
}
}
输出结果:
23564911525446
7