使用数字位数打印范围内的所有回文数 [英] Printing All Palindrome Numbers Within A Range Using The Number Of Digits Of The Numbers

查看:97
本文介绍了使用数字位数打印范围内的所有回文数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我写了我的Palindrome程序,该程序应该打印该范围之间的所有回文数,但是似乎只打印0到9,这意味着它将所有其他数都评估为false.我不明白为什么?

So I wrote my Palindrome program that is supposed to print all palindrome numbers between the range but it only seems to print 0 to 9 which means its evaluating all other numbers as false. I dont understand why?

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


}

推荐答案

我的第一个建议是记录各种方法的输出,以确保获得正确的输出.使用System.out.println()可以帮助解决此问题,因为我看到您无权访问调试器.

My first suggestion would be to log the output of your various methods to ensure you are getting back the correct output. Using System.out.println() can help with this as I see you don't have access to a debugger.

同样,对于您的isPalindrome()函数.我不知道您在使用数学函数的位置上是否有任何限制,但是您可以在这些地方做一些事情:

Also, as for your isPalindrome() function. I don't know if you have any restrictions where you have to use math functions but you could do something along these lines there:

public static boolean isPalindrome(int number) {
    String numString = number + "";
    for(int i = 0; i < numString.length() / 2; i++){
        if(numString.charAt(i) != numString.charAt(numString.length() - i){
            return false;
        }
    }
    return true;
}

这实际上检查了数字的字符串表示形式中的每个字符是否与字符串结尾处的相应字符相对应.您可以将条件除以2,因为在中点之后,您已经检查了字符的相等性.

This essentially checks each character in the String representation of your number against it's corresponding character at the end of the String. You can divide the condition by 2 because after the half way point you have already checked the equality of the characters.

警告,在此方面我可能有一个错误.我没有测试.我只是想提供一些想法和起点.

Warning, I may have some off by one errors in this. I didn't test it. I'm just trying to give some ideas and a starting point.

祝你好运!

这篇关于使用数字位数打印范围内的所有回文数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆