2 回答

TA贡献1842条经验 获得超21个赞
好吧,这意味着您的数组不在函数的上下文中,因此它无法“看到”它。您要么必须在函数中声明它,要么通过类中的某个字段访问它,要么将其作为参数传递
public static void print(int[] nums, int count)、 {...}

TA贡献1860条经验 获得超8个赞
“名称‘(数组名称)’在当前上下文中不存在”
由于错误表明您的函数无法在函数上下文中找到数组(因为它在函数本身之外)。
为了找到它,您有两种选择:
print(counter, nums)
public static void print(int count, int[] nums)
{...}
或者将所有内容包装在一个类中:
class Program
{
static int[] nums;
static void Main(string[] args)
{
int counter = int.Parse(Console.ReadLine());
int[] nums = new int[counter];
while (counter > 0)
{
nums[counter] = counter;
counter--;
}
print(counter);
}
public static void print(int count)
{
// some code
while (count > 0)
{
Console.WriteLine(nums[count]); //line with the error
count--;
}
}
}
- 2 回答
- 0 关注
- 85 浏览
添加回答
举报