程序运行出错
今天在推荐帖上看到一个面试算法题,感觉很好想拿来学习一下,可是按照帖子楼主的方法总是出错,所以请各位大侠帮忙看看问题出在哪里。
题意大概是:
     对字符串进行排序,用任意一种编程语言来实现,不能使用现有的类,在排序中,字符串“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静态后,却并不能显示出正确的排序结果。请大家帮忙看看问题究竟出在哪里吧!谢谢了先!
------解决方案--------------------你的Sort()是实例方法,不能这样直接在静态方法中使用。
private static void Sort(string[] s)  
------解决方案--------------------public static void Main()
方法修饰符public去掉
或者方法修饰符全部改成public
------解决方案--------------------也可以把调用的地方改成:
Sort08 s = new Sort08();
s.Sort(str);
------解决方案--------------------C# code
public static void Main() 
    { 
        string[] str = { "dad", "bood", "bada&quo