2 回答

TA貢獻1842條經驗 獲得超22個贊
好吧,這意味著您的數組不在函數的上下文中,因此它無法“看到”它。您要么必須在函數中聲明它,要么通過類中的某個字段訪問它,要么將其作為參數傳遞
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 關注
- 108 瀏覽
添加回答
舉報