1到11有4个“1”,1中一个“1”,10中一个“1”,11中两个“1”,1000中,有多少个“1”

package test07;   
  
public class CountOne {   
    public static void main(String[] args) {   
        int count = 0;   
        for (int i = 1; i <= 1000; i++) {   
            String s = String.valueOf(i);   
            for (int j = 0; j < s.length(); j++) {   
                if (s.charAt(j) == '1')   
                    count++;   
            }   
        }   
        System.out.println(count);   
    }   
}  

 

你可能感兴趣的:(J#)