简体   繁体   English

C# 错误:“命名空间不能直接包含字段或方法等成员”

[英]C# Error: "A namespace cannot directly contain members such as fields or methods"

A method receiving 2 numbers and I need to return the numbers of common digits for them.一种接收 2 个数字的方法,我需要为它们返回常用数字的数量。 For example, the numbers 2201 and 3021 returns 3 because these numbers have 3 common digits: 0, 1, and 2.例如,数字22013021返回3 ,因为这些数字有 3 个常见数字:0、1 和 2。

I get this error and don't understand it: A namespace cannot directly contain members such as fields or methods我收到此错误,但不明白: A namespace cannot directly contain members such as fields or methods

Here is the code:这是代码:

public static int AlphaRomeo(int a, int b)
{
    int count = 0;
    while (a > 0)
    {
        int tempa = a % 10;
        a = a / 10;
        int tempb = b % 10;
        b = b / 10;
        if (tempa == tempb)
            count++;
    }
    return count;

}

屏幕截图

Might be easier to avoid the mathematics:可能更容易避免数学:

private static string _digits = "0123456789";

public static int AlphaRomeo(int a, int b)
{
    string aStr = a.ToString();
    string bStr = b.ToString();
    int common = 0;

    for (int i = 0; i < _digits.Length; i++)
    {
        if (aStr.Contains(_digits[i]) && bStr.Contains(_digits[i]))
            common++;
    }

    return common;
}

Consider this example, you don't have a class in your code:考虑这个例子,你的代码中没有 class :

using System;  // Imports

namespace LearnClasses    // Namespace
{
    class Program    //class
    {
        static void Main(string[] args)   // Method 1
        {
           Console.WriteLine( AlphaRomeo(2201, 3021));
        }

        public static int AlphaRomeo(int a, int b)  // Method 2
        {
            int count = 0;
            // Your code
            return count;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c#名称空间不能直接包含成员,例如字段或方法 - c# a namespace cannot directly contain members such as fields or methods C#名称空间不能直接包含诸如void的字段或方法之类的成员 - C# A namespace cannot directly contain members such as fields or methods for void C# 代码显示错误“命名空间不能直接包含成员,如字段或方法” - C# code showing error "A namespace cannot directly contain members such as fields or methods" 命名空间不能直接包含成员,如字段或方法? - A namespace cannot directly contain members such as fields or methods? 错误:名称空间不能直接包含诸如字段或方法之类的成员 - Error: A namespace cannot directly contain members such as fields or methods 错误4名称空间不能直接包含成员,例如字段或方法 - Error 4 A namespace cannot directly contain members such as fields or methods “名称空间不能直接包含诸如字段或方法之类的成员” File:具有上下文的控制器 - “A namespace cannot directly contain members such as fields or methods” File: controller with context 命名空间不能直接包含字段或方法等成员。 - A namespace cannot directly contain members such as fields or methods. 错误:名称空间不直接包含诸如字段或方法之类的成员 - Error :A namespace does not directly contain members such as fields or methods 错误“命名空间不能直接包含字段或方法等成员”我的代码有什么问题? [等候接听] - Error “A namespace cannot directly contain members such as fields or methods” what's wrong with my code? [on hold]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM