添加数字直到回文

时间:2014-03-14 19:17:18

标签: c# palindrome

我一直在研究C#控制台应用程序,并且已成功根据用户输入int进行了回文检查。我现在遇到了涉及添加用户输入和所述输入反向的情况的问题。我希望通过此添加继续检查回文编号,然后在到达时停止执行程序。我的程序似乎很难保存反转数字的状态,任何帮助都会非常感激:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MathPalindromeConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, temp, remainder, reverse = 0;
            Console.WriteLine("Enter an integer \n");
            num = int.Parse(Console.ReadLine());
            bool palindromic = true;
            temp = num;
                while (num > 0)
                {
                    remainder = num % 10;
                    reverse = reverse * 10 + remainder;
                    num /= 10;
                }
                Console.WriteLine("Given a number is = {0}", temp);
                Console.WriteLine("Its reverse is = {0}", reverse);
                while (palindromic)
                {
                    if (temp == reverse)
                    {
                        Console.WriteLine("Number is a palindrome \n");
                        palindromic = false;
                        Console.ReadLine();
                    }
                    if (temp != reverse)
                    {
                        Console.WriteLine("Number is not a palindrome \n");
                        Console.WriteLine(temp += reverse);
                        Console.ReadLine();
                    }
                }
            }
        }
}

1 个答案:

答案 0 :(得分:-1)

我打算和L.B.说同样的话。他把它放在评论中,所以我把它放在这里。为什么不使用:

bool pal = num.ToString() == new string(num.ToString().Reverse().ToArray());