2012-08-01 50 views
5
功能

所以我有這樣的代碼在這裏:C#調用具有多個值

int n; 
public static void Main(string[] args) 
    { 
     Console.Write("Please insert a number : "); 
     n = int.Parse(Console.ReadLine()); 
     Console.Write("Please insert wait time (0,1 or 2) : "); 
     int time = int.Parse(Console.ReadLine())*1000;    
     Calculate(n,time);      
    } 

什麼是我打電話給那個計算爲n倍的值(後給出一個(N,時間)功能的最佳方法其他),但同一時間。我已經想過使用一個數組來存儲多個n值,但是有更好的選擇。

另外我想傳遞多個n作爲參數從命令行。

任何想法? 在此先感謝!

+0

如果你只需要幾個n值,那麼元組可能比數組更好。 – 2012-08-01 08:28:14

回答

7

您只需使用params屬性。

public void Calculate(time, params int[] parameters){ ... } 

這將允許您撥打:

Calculate(time, 1, 2, 3, 4, 5, 6, ....) 

在功能,您可以遍歷:

foreach(int item in parameters){} 
0

數組應該很好地工作。

public void Calculate(int[] numbers, int time) 
{ ... } 

還與LINQ可以執行不同的選擇您的數組的:

Calculate(n.Distinct().ToArray(), time); 
1
// 
private void Calculate(int int_value, param int[] int_array_value) 
{ 
` enter code here`// your code goes here 
}