求教C#中的一个简单问题:“Test.max(int, int)”是“方法”,但此处被当做“类型”来使用

2025-03-05 10:16:39
推荐回答(3个)
回答1:

using System;
class Test
{
public static int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
static public void Main()
{
Console.WriteLine("the max of 6 and 8 is:{0}", max (6,8));
}
}
存在的问题 1 max是个方法,你这里把它当成了一个类来创建对象,
2 静态方法只能调用静态方法,所以要在max方法前面声明其为静态

回答2:

class Test
{
// max 是一个方法,而且要在 static 型的main方法中被调用,因此也要加上 static 关键字
public static int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
static void Main()
{
Console.WriteLine("the max of 6 and 8 is:{0}", max(6, 8)); //直接调用max方法
}
}

回答3:

天啊,直接改成 int n = max(6,8);
就行了。
或者直接用
Console.WriteLine("the max of 6 and 8 is:{0}", max(6,8));
这一句就够了。