【问题标题】:I don't understand what I changed我不明白我改变了什么
【发布时间】:2012-03-06 17:23:39
【问题描述】:

一个小时前,我花了很多时间完成了这个项目,但是我在正确保存它时遇到了问题并且完全丢失了它。但是,我又做了一次,但这次它不起作用

我不确定这次我做错了什么...我想我做的完全一样,但是这次不行?

我收到以下错误:

错误 1 ​​非静态字段需要对象引用, 方法或属性 'ConsoleApplication1.Program.convertir(string)' C:\Documents 和 Settings\modifiedForPersonalReasons\Program.cs 12 45 ConsoleApplication1

这是我的代码,我打算只输入错误行,但它可能会遗漏一些我们可能需要发现问题的信息:

    static void Main(string[] args)
    {
        Console.WriteLine("23.21 es " + convertir("23.21"));
        Console.ReadLine();
    }

    public string convertir(string str)
    {
        string[] arr;
        arr = str.Split('.');

        string rtn = españolizar(arr[0], "peso");

        if (arr.Length > 1)
        {
            rtn += " con " + españolizar(arr[1], "centavo");
        }

        return rtn;
    }

    public string españolizar(string str, string str2)
    {
        string[] list1 = { "cero", "un", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce", "trece", "catorce", "quince" };
        string[] list2 = { "nivelarindexes", "dieci", "veinti", "trei", "cuare", "cincue", "sese", "sete", "oche", "nove" };

        int numero = int.Parse(str);
        string strNumero = Convert.ToString(numero);

        int primerDigito = int.Parse(Convert.ToString(strNumero[0]));
        int segundoDigito = 0;

        if (strNumero.Length > 1)
        {
            segundoDigito = int.Parse(Convert.ToString(strNumero[1]));
        }

        int cases = -1;

        if (numero > -1 && numero < 16)
        {
            cases = 0;
        }
        else if (numero > 15 && numero < 30)
        {
            cases = 1;
        }
        else if (numero > 29 && numero < 100)
        {
            cases = 2;
        }
        else if (numero == 100)
        {
            cases = 3;
        }

        string rtn = "";

        switch (cases)
        {
            case 0:
                rtn = list1[numero] + " " + str2;
                if (primerDigito != 1)
                {
                    rtn += "s";
                }
                break;
            case 1:
                if (numero != 20)
                {
                    rtn = list2[primerDigito] + list1[segundoDigito];
                }
                else
                {
                    rtn = "veinte";
                }
                rtn += " " + str2 + "s";
                break;
            case 2:
                rtn = list2[primerDigito] + "nta";
                if (segundoDigito != 0)
                {
                    rtn += " y " + list1[segundoDigito];
                }
                rtn += " " + str2 + "s";
                break;
            case 3:
                rtn = "cien " + str2 + "s";
                break;
            case -1:
                rtn = "número invalido";
                break;
        }

        rtn = rtn.Replace("iun", "iún").Replace("idos", "idós").Replace("itres", "itrés").Replace("iseis", "iséis");

        return rtn;
    }

我可以发誓我没有改变任何东西:C

【问题讨论】:

    标签: c# compiler-errors


    【解决方案1】:

    public string convertir(string str) 更改为public static string convertir(string str)。对 españolizar 函数执行相同操作。

    【讨论】:

    • Españolizar 函数也需要同样的改变,不是吗?
    • 23.21 es veintitrés pesos con veintiún centavos :D。谢谢
    【解决方案2】:

    您收到此消息是因为您正在从静态方法调用非静态方法。您需要一个此类的实例来调用非静态方法,或将其他方法设为静态。

    【讨论】:

      【解决方案3】:

      您必须将两个方法更改为static 方法,因为您不能从非静态方法调用静态方法。 请参阅 here 了解原因。

      要解决此问题,您必须更改以下内容:

      public string convertir(string str) 
      

      public static string convertir(string str)
      

      然后改变

      public string españolizar(string str, string str2)
      

      public static string españolizar(string str, string str2)
      

      【讨论】:

        【解决方案4】:

        convertir 方法需要是静态的(与 Main 相同)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-21
          • 2013-05-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多