所谓回文,是指从左到右念跟从右到左念是一样的,例如:1223221,yujajuy,以下是判断回文的算法
public static void JudgePalindrome (string teststring)
{
for (int i = 0; i < teststring.Length - 1; i++)
        {
            if (teststring[i] != teststring[teststring.Length - i - 1])
            {                Console.WriteLine("It is palindrome");
            }
            else
            {
                Console.WriteLine("It is not palindrome");
            }
        }
        
}
判断成功后,输出了很多句It is palindrome或者是It is not palindrome,怎样让它判断完成之后就只输出一句It is palindrome或者是It is not palindrome呢

解决方案 »

  1.   

    class MainClass
    {
        public static bool JudgePalindrome(string teststring)
        {
            bool flag = true ;
            for (int i = 0; i < teststring.Length - 1; i++)
            {
                if (teststring[i] != teststring[teststring.Length - i - 1])
                {
                    flag = false;
                    break;
                }
            }
            return flag;
        }    public static void Main(string[] args)
        {
            bool flag = JudgePalindrome("aba");        if(flag)
                Console.WriteLine("It is palindrome");
            else
                Console.WriteLine("It is not palindrome");
        }
    }