取出每个数的各个数位分别与2进行比较即可
public class Main {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i < 2021; i++){
int temp = i;
while(temp != 0){
if(temp % 10 == 2){
count++;
}
temp /= 10;
}
}
System.out.println(count); //624
}
}