2017-03-16 53 views
-2

如何在3秒內將此結果打印到控制檯?我以前的問題是過於具體的靜態方法錯誤,而不是優化部分....如何在3秒內寫入200萬條記錄?

我需要能夠在控制檯上以3秒的特定方式打印這種陣列的組合。

using System; 

namespace MelodiousPassword 
{ 
    private static int _n; 
    static void Main(string[] args) 
    { 
     _n = Convert.ToInt32(Console.ReadLine()); 
     string[] c = { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "z" }; 
     string[] v = { "a", "e", "i", "o", "u" }; 
     Passwords("", c, v); 
     Passwords("", v, c); 
    } 

    static void Passwords(string w, string[] a, string[] b) 
    { 
     if (w.Length == _n) 
      Console.WriteLine(w); 
     else 
      foreach 

      (var l in a) { Passwords(w + l, b, a); } 
    } 
} 
+0

您無法從靜態方法引用非靜態屬性,請參閱http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context –

+0

[你可以訪問實例變量,如果你真的想](http://stackoverflow.com/questions/3371839/is-it-possible-to-access-an-instance-variable-via-a-static-method )但我建議讓n也是靜態的。 – mbx

+1

@TiesonT。你的「重複」是[標籤:java],而不是[標籤:c#] - 雖然在這種情況下,兩者行爲相似,我通常建議不要參考另一種語言的答案。 – mbx

回答

1

由於Main方法是一個靜態方法,你也應該在n變量更改爲靜態的。這樣做:

internal static int n; 

除此之外,它似乎你會有另一個錯誤。您將4個參數傳遞給僅有3個參數的Passwords方法。

希望它有幫助!

+0

謝謝,這有幫助! – Matt