【发布时间】:2022-01-06 11:51:42
【问题描述】:
如何将具有未知数量整数的数组传递给函数? 谁能告诉我我做错了什么?
尝试运行代码时出现以下错误:
错误 CS1501 方法“解决方案”没有重载需要 6 个参数
using System;
namespace IntegerTest
{
class Program
{
public static int Solution(int[] input)
{
Array.Sort(input);
int index = 0;
// Skip negatives
while (index < input.Length && input[index] < 1)
index++;
int expected = 1;
while (index < input.Length)
{
if (input[index] > expected)
return expected;
// Skip number and all duplicates
while (index < input.Length && input[index] == expected)
index++;
expected++;
}
return expected;
}
public static void Main()
{
Console.WriteLine(Solution( 1, 3, 6, 4, 1, 2));
}
}
}
【问题讨论】:
-
您需要在数组前加上
params关键字才能接受可变参数样式输入;否则,传入一个 int 数组,你现在只是传入一堆独立的 int