1
因此,我編寫了我的迴文編程程序,該程序應該打印範圍內所有迴文數字,但它似乎只打印0到9,這意味着它評估所有其他數字爲假。我不明白爲什麼?使用數字位數打印所有迴文數字
public class Palindrome
{
public static void main(String [] args)
{
int number = 0;
final int END = 10000;
while (number <= END)
{
if (isPalindrome(number))
{
System.out.print(" " + number);
}
number++;
}
}// end of main method
public static boolean isPalindrome(int number)
{
//local variables
int counter = 0;
int numDigits = 0;
final int END = 10000;
boolean palindrome = false;
while (number <= 10000)
{
//formula for finding the number of digits in a number
numDigits = getNumDigits(number);
//special case if number = 0
if (numDigits == 1)
{
palindrome = true;
break;
}
if(counter < numDigits/2)
{
if (((number/(int) Math.pow(10, counter)) % 10) != (number/(int) Math.pow(10, numDigits - counter - 1)))
{
palindrome = false;
counter++;
}
else
{
palindrome = true;
}
}
number++;
}// end of loop
return palindrome;
}//end of isPalindrome Method
public static int getNumDigits(int number)
{
//local variables
int numDigits = (int) (Math.log10(number) + 1);
if(number == 0)
{
numDigits = 1;
}
return numDigits;
}//end of getNumDigits method
}
啓動您的調試器並親自查看。 –
我沒有調試器 –
然後得到一個正常的IDE(如Eclipse)。免費。 –