2017-02-23 31 views
-6

您好我有以下的代码,它给我的错误,回文(字符串)是一种方法,在给定的方法无效。一个函数,检查给定的单词是否回文

请帮助解决问题

namespace justtocheck 
{ 
    public class Program 
    { 
    public static bool Palindrome(string word) 
    { 
     string first = word.Substring(0, word.Length/2); 
     char[] arr = word.ToCharArray(); 
     Array.Reverse(arr); 
     string temp = new string(arr); 
     string second = temp.Substring(0, temp.Length/2); 
     return first.Equals(second); 

     //throw new NotImplementedException("Waiting to be implemented."); 
    } 
    public static void Main(string[] args) 
    { 
     Console.WriteLine(Palindrome.IsPalindrome("Deleveled")); 
    } 
} 
} 
+1

有这个代码不'IsPalindrome()'方法。 – Claies

+0

嗨,仍然显示一些错误,请参考testdome.com/questions/c-sharp/palindrome/7282?visibility=1 –

+0

你不会学习如何编程,要求人们为你写代码测试问题的答案.... – Claies

回答

2

你声明的方法和调用未声明的类的方法。 正确

Console.WriteLine(Palindrome("Deleveled")); 

或者改变你的方法声明

public class Palindrome 
{ 
    public static bool IsPalindrome(string word) 
    { 
     string first = word.Substring(0, word.Length/2); 
     char[] arr = word.ToCharArray(); 
     Array.Reverse(arr); 
     string temp = new string(arr); 
     string second = temp.Substring(0, temp.Length/2); 
     return first.Equals(second); 
     //throw new NotImplementedException("Waiting to be implemented."); 
    } 
} 
+0

非常感谢你 –

+0

嗨,仍然显示一些错误请参考https://www.testdome.com/questions/c-sharp/palindrome/7282?visibility=1 –

0

这是很好的和简单的:

public static bool Palindrome(string word) 
{ 
    var w = word.ToLowerInvariant(); 
    return w.Zip(w.Reverse(), (x, y) => x == y).Take(word.Length/2).All(x => x); 
} 
相关问题