如何將具有未知整數數量的陣列傳遞給函式?誰能告訴我我做錯了什么?
嘗試運行代碼時出現以下錯誤:
錯誤 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));
}
}
}
uj5u.com熱心網友回復:
您可以使用陣列引數呼叫函式(例如Solution(new[] {1, 3, 6, 4, 1, 2}),或修改函式簽名以采用params引數 ( int Solution(params int[] input))。
uj5u.com熱心網友回復:
您的方法接受 a int[],因此創建一個new int[]
Solution(new int[] {1, 3, 6, 4, 1, 2});
uj5u.com熱心網友回復:
您將 6 個引數傳遞給一個需要一個引數的方法。將您的主要方法更改為如下所示:
public static void Main()
{
int[] arr = { 1, 3, 6, 4, 1, 2};
Console.WriteLine(Solution(arr));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/369782.html
上一篇:OpenMDAO中的輸出大小
