如下,判断一个数是不是self-descriptive(如2020,第零位上数字等于零在数串中出现的次数),结果是没有任何东西print出来。为什么会这样。。。
Boolean selfor = true;
int j = 0;
if (digits<=10){
while(j<=digits-1){
int count = 0;
int k = 0;
while(k<=digits-1){
if (entered.charAt(k)==j){
count++;
}
k++;
}
if(entered.charAt(j)==count){
selfor = true;
}
else{
selfor = false;
break;
}
j++;
}
if(selfor==true)
System.out.println(number+" is a Self-Descriptive Number");
}
------解决思路----------------------
package topics_390908485;
import java.util.*;
/**
* A self-descriptive number is an integer with the property that, when digit
* positions are labeled 0 to N-1, the digit in each position is equal to the
* number of times that that digit appears in the number.
*
* For example,
* 2020 is a self-descriptive number because:
* 0123[digit positions are labeled 0 to N-1]
* position "0" has value 2 and there is two 0 in the number
* position "1" has value 0 because there are not 1's in the number
* position "2" has value 2 and there is two 2
* position "3" has value 0 and there are zero 3's
*/
public class SelfDescriptiveNumber {
public static void main(String[] args) {
for (Integer ii = 0; ii <= 3211000; ii++) {
if (isSelfDescriptiveNumber(ii.toString())) {
System.out.println("[" + getDateTime() + "]" + ii
+ " is a self-descriptive number");
}
}
for (Integer ii = 0; ii <= 3211000; ii++) {
if (isSelfDescribing(ii)) {
System.out.println("[" + getDateTime() + "]" + ii
+ " is a self-descriptive number");
}
}
// test case refers to
// [http://en.wikipedia.org/wiki/Self-descriptive_number]
printTestResult("42101000");
printTestResult("521001000");
printTestResult("6210001000");
}
public static boolean isSelfDescriptiveNumber(String number) {
try {
int[] digitValueArray = new int[number.length()];
int[] digitTimesArray = new int[number.length()];
for (int index = 0; index < number.length(); index++) {
digitValueArray[index] = Character.digit(number.charAt(index),
10);
digitTimesArray[digitValueArray[index]]++;
}
return Arrays.equals(digitValueArray, digitTimesArray);