1

I already wrote the part of the program that reverses the number:

        int n = 0;
        n = Convert.ToInt32(textBox1.Text);
        double l = n;
        double reverse = 0;
        while (left > 0)
        {
            double r = left % 10;
            reverse = reverse * 10 + r;
            l = l / 10;
        }

        double final = n + rev;

However, if the final double does is not a palindrome, I want the program to add the final to its palindrome, and keep doing that until the a palindrome is found. The problem is in the the conditional statement.What condition should I give to the if statement?

4

2 回答 2

2

我认为您可以将字符串反转而不是将其视为数字:

using System.Linq;

var N = textBox1.Text;
var reversedN = new string(n.Reverse().ToArray());
var result = Convert.ToInt32(reversedN);
于 2013-08-09T01:01:23.250 回答
1

正如在这个线程中

bool isPalindrome(String s)
{
    char[] charArray = s.ToCharArray();
    Array.Reverse( charArray );
    return s.equals(new string(charArray));
}

对字符串进行回文检查。如果它不是回文,则将字符串转换为 int,添加您想要的任何内容,将其转换回字符串,重复。

避免double在此应用程序中使用。坚持int

于 2013-08-09T01:18:11.970 回答