继续倒转号码。并将其添加到原始编号中。直到出现回文数

Keep Reversing the no. & adding it to the original no. until there is a Palindrome Number

不是"Find the Palindrome of a Number"!它比那有点扭曲:

第1步:反转原始数字的数字。

第 2 步:添加反向数字。

第 3 步:如果新数字是回文数字,则打印输出。

限制为 15 步,如果它本身是回文,我们可以打印原始数字。我们必须在这个程序中使用函数调用。在这里,我使用了两个函数 () - 第一个是反转数字,第二个是将原始数字和反转后的数字相加,并检查总和是否为回文。

如果我能以任何方式改进这段长代码,请提出您的意见。

PS:Java 的新手,尤其是函数调用!

谢谢!!这是代码,有点长!对不起:(

import java.util.*;
public class PalindromicNumber
{
 public int PalinReverse(int n)
    {
     int n1=n;
     int x1=0, d1=0;
      while (n1>0)//just used to store the reverse of the original number
        {
          d1=n1%10;
          x1=(x1*10)+d1;
          n1=n1/10;
        }
    return x1;
     }

    public int PalinCheck (int n, int p)
      {
         int F=0;
         F=n+p; 
         int n1=F, x1=0, d1=0;

          while(n1>0)//checks if the sum of reversed no. and the original number is palindrome or not
           {
            d1=n1%10;
            x1=(x1*10)+d1;
            n1=n1/10;
           }
   if (x1==F)
       {
       System.out.println("The number"+ F +"is a Palindrome");
       return 1;
       }
   else 
       return F; //returns the sum if it is not a palindrome
    }

   public static void main (String args[])
     {
      Scanner sc=new Scanner(System.in);
      PalindromicNumber ob=new PalindromicNumber();

      System.out.println("Enter the original number");
      int n=sc.nextInt();

      int count=0;
      int n1=n, x1=0, d1=0;
        while(n1>0) //this checks if the original no. is a palindrome or not
           {
            d1=n1%10;
            x1=(x1*10)+d1;
            n1=n1/10;
           }

        if (x1==n)
        System.out.println("The original number="+n+"is a palindrome number");

        else

        for (count=0;count<15;count++)
           {
               int a=ob.PalinReverse(n);
               System.out.println("The reversed number is"+a);
               int b=ob.PalinCheck(n,a);
                if(b==1)
                {
                    System.out.println("The no. of steps it took was"count+1);
                    break;// the palindromic no. is now found out
                } 
                else 
                     n=b;//used to update the value of n
           }
    }
}

问题出在您的 PalinReverse 方法中。您的 while 循环是 运行 无限循环,因为条件始终为真。

替换

while (n>0)

while (n1>0)

您还应该学习如何调试程序。之后你的生活会轻松10倍