今天在推荐帖上看到一个面试算法题,感觉很好想拿来学习一下,可是按照帖子楼主的方法总是出错,所以请各位大侠帮忙看看问题出在哪里。题意大概是:
    对字符串进行排序,用任意一种编程语言来实现,不能使用现有的类,在排序中,字符串“Bc”,“Ad”,“aC”,“Hello”,“X man”,“little”,“During”,“day”能够排序成 “Ad”,"aC",“Bc”,“During”,“day”,“Hello”,“little”,“X man”,也就是说,在排序的过程并不是传统的按照字符串排序,在排序中还需要将小写字母一并排序,也就是说a字符串要在B或b之前。源程序:
using System;class Sort08
{
    public static void Main()
    {
        string[] str = { "dad", "bood", "bada", "Admin", "Good", "aete", "cc", "Ko", "Beta", "Could" };
        Console.WriteLine("Original string is :");
        for (int i = 0; i < str.Length; i++)
        {
            Console.Write(str[i] + " ");
        }
        Console.WriteLine();        Sort(str);   //报错为“非静态的字段、方法或属性“Sort08.Sort(str)”要求对象引用”
        Console.WriteLine("After Sort it gives:" + str);        Console.ReadLine();
    }    private void Sort(string[] s)
    {
        for (int i = 0; i < s.Length; i++)
        {
            for (int j = 0; j < s.Length - i - 1; j++)
            {
                if (Compare(s[j], s[j + 1]) > 0)
                {
                    string tem = s[j];
                    s[j] = s[j + 1];
                    s[j + 1] = tem;
                }
            }
        }
    }    private int Compare(string str1, string str2)
    {
        int x = 0;
        for (int i = 0, j = 0; (i < str1.Length) && (j < str2.Length); i++, j++)
        {
            int s1 = (int)str1[i];
            int s2 = (int)str2[j];            //insert 
            if (s1 >= 97)
            {
                s1 -= 32;
            }            if (s2 >= 97)
            {
                s2 -= 32;
            }
            //end             if (s1 > s2)
            {
                x = 1;
                break;
            }
            else if (s1 < s2)
            {
                x = 0;
                break;
            }
            else if (s1 == s2)
            {
                if ((int)str1[i] > (int)str2[j])
                {
                    x = 1;
                    break;
                }
                else
                {
                    x = 0;
                    break;
                }
            }
        }
        return x;
    }
}错误如上红色部分标注的那样,但我若是把下面两个方法都改成Static静态后,却并不能显示出正确的排序结果。请大家帮忙看看问题究竟出在哪里吧!谢谢了先!

解决方案 »

  1.   

    你的Sort()是实例方法,不能这样直接在静态方法中使用。
    private static void Sort(string[] s) 
      

  2.   

    public static void Main()
    方法修饰符public去掉
    或者方法修饰符全部改成public
      

  3.   

    也可以把调用的地方改成:Sort08 s = new Sort08();
    s.Sort(str); 
      

  4.   

    先谢谢楼上诸位的解答,但是按照各位的说法我都试验了一下,虽然程序运行无错了,可问题正如帖子最后一句说的那样“但我若是把下面两个方法都改成Static静态后,却并不能显示出正确的排序结果。”
    程序运行结果如下:
    Original string is :
    dad,bood,bada,Admin,Good,aete,cc,Ko,Beta,Could
    After Sort it gives:System。string[]上面标注的红色就不对了,应该显示的是重新排序后的结果,但是现在只显示了System。string[]这样的形式,所以还请各位帮忙看看问题究竟出在哪里。
      

  5.   

            Sort(str);  //报错为“非静态的字段、方法或属性“Sort08.Sort(str)”要求对象引用”这里都说了错的原因使用方法Sort08 s=new sort08();
    s.sort(str);这样就可以了在静态方法中调用不是静态的方法,就要实例对像
      

  6.   

    的确,6楼、7楼的朋友说的很正确,但是按照这种办法使用了之后程序还是出现如下错误:程序运行结果如下: 
    Original string is : 
    dad,bood,bada,Admin,Good,aete,cc,Ko,Beta,Could 
    After Sort it gives:System.string[] 上面标注的红色就不对了,应该显示的是重新排序后的结果,但是现在只显示了System.string[]这样的形式,现在主要的问题卡在这儿了。所以还请各位帮忙看看这到底是什么原因。
      

  7.   


    public static void Main() 
        { 
            string[] str = { "dad", "bood", "bada", "Admin", "Good", "aete", "cc", "Ko", "Beta", "Could" }; 
            Console.WriteLine("Original string is :"); 
            for (int i = 0; i < str.Length; i++) 
            { 
                Console.Write(str[i] + " "); 
            } 
            Console.WriteLine(); 
            Sort08 s=new sort08(); 
            str.lear()
            s.sort(str); 
            //Sort(str);  //报错为“非静态的字段、方法或属性“Sort08.Sort(str)”要求对象引用” 
            
             //Console.WriteLine("After Sort it gives:" + str);
             
            for (int i = 0; i < str.Length; i++) 
            { 
                Console.Write(str[i] + " "); 
            } 
            Console.ReadLine(); 

        } 
      

  8.   

    楼主排序好了,但没有再次输出来.
     class Sort08
        {
            public static void Main()
            {
                string[] str = { "dad", "bood", "bada", "Admin", "Good", "aete", "cc", "Ko", "Beta", "Could" };
                Console.WriteLine("Original string is :");
                for (int i = 0; i < str.Length; i++)
                {
                    Console.Write(str[i] + " ");
                }
                Console.WriteLine();
                Sort08 a = new Sort08();
                a.Sort(str);  //报错为“非静态的字段、方法或属性“Sort08.Sort(str)”要求对象引用” 
                Console.WriteLine("After Sort it gives:" + str);
                for (int i = 0; i < str.Length; i++)
                {
                    Console.Write(str[i] + " ");
                }            Console.ReadLine();
            }        public  void Sort(string[] s)
            {
                for (int i = 0; i < s.Length; i++)
                {
                    for (int j = 0; j < s.Length - i - 1; j++)
                    {
                        if (Compare(s[j], s[j + 1]) > 0)
                        {
                            string tem = s[j];
                            s[j] = s[j + 1];
                            s[j + 1] = tem;
                        }
                    }
                }
            }        private int Compare(string str1, string str2)
            {
                int x = 0;
                for (int i = 0, j = 0; (i < str1.Length) && (j < str2.Length); i++, j++)
                {
                    int s1 = (int)str1[i];
                    int s2 = (int)str2[j];                //insert 
                    if (s1 >= 97)
                    {
                        s1 -= 32;
                    }                if (s2 >= 97)
                    {
                        s2 -= 32;
                    }
                    //end                 if (s1 > s2)
                    {
                        x = 1;
                        break;
                    }
                    else if (s1 < s2)
                    {
                        x = 0;
                        break;
                    }
                    else if (s1 == s2)
                    {
                        if ((int)str1[i] > (int)str2[j])
                        {
                            x = 1;
                            break;
                        }
                        else
                        {
                            x = 0;
                            break;
                        }
                    }
                }
                return x;
            }
        }