代码如下:
package com.moson.interview;
/**
* 给定一个整数,按10进制来看,计算里面包含多少个0
* @author moxingjian
* @version 1.0
* @date 10/22/19 10:41 PM
*/
public class CountZero {
public static void main(String[] args) {
// 假定一个整数
int inputValue = -20191020;
int value = inputValue;
// 统计有多少个0
int count = 0;
if (value == 0) {
count ++;
} else {
// 如果值小于0,那么就要取绝对值
value = value < 0 ? -value : value;
while (value > 1) {
if (value % 10 == 0) {
count++;
}
value = value / 10;
}
}
System.out.printf("整数 %d 含有 %d 个0", inputValue, count);
}
}