本帖最后由 刘蕴学 于 2012-6-30 18:04 编辑
我也凑个热闹- public class Count
- {
- public static void main(String[] args)
- {
- System.out.println("出现总次数为:" + getValueCount(0, 99, 5));
- }
-
- public static int getValueCount(int begin, int end,int num)
- {
- int tmp = begin;
- int count = 0;
- while(true)
- {
- if(tmp % 10 == num)
- count++;
- if(tmp < 10)
- {
- if(0 != count)
- System.out.println("Value=" + begin + " Count=" + count);
- if(end == begin)
- return count;
- else
- return count + getValueCount(begin + 1, end, num);
- }
- tmp /= 10;
- }
- }
- }
复制代码- 控制台输出
- Value=5 Count=1
- Value=15 Count=1
- Value=25 Count=1
- Value=35 Count=1
- Value=45 Count=1
- Value=50 Count=1
- Value=51 Count=1
- Value=52 Count=1
- Value=53 Count=1
- Value=54 Count=1
- Value=55 Count=2
- Value=56 Count=1
- Value=57 Count=1
- Value=58 Count=1
- Value=59 Count=1
- Value=65 Count=1
- Value=75 Count=1
- Value=85 Count=1
- Value=95 Count=1
- 出现总次数为:20
复制代码 |