[英]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.例如,数字
2201
和3021
返回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.