如何将构造函数的参数设置为可变大小的数组?

时间:2013-05-10 23:34:20

标签: c# arrays parameters constructor

家庭作业:我们正在制作一个刽子手计划。我有以下方法:

    public char[] selectWord()
    {
        String[] hangmanWords = new String[50] { "time", "person", "year", "way", "day", "thing", "man", "world", "life", "hand", "part", "child", "eye", "woman", "place", "work", "week", "case", "point", "government", "company", "number", "group", "problem", "fact", "good", "new", "first", "last", "long", "great", "little", "own", "other", "old", "right", "big", "high", "different", "small", "large", "next", "early", "young", "important", "few", "public", "bad", "same", "able" };
        Random wordIndex = new Random();
        int randomIndex = wordIndex.Next(0, 49); 
        wordToGuess = hangmanWords[randomIndex].ToUpper();
        char[] wordToGuessArray = wordToGuess.ToCharArray();                                         // convert word to array of letters
        return wordToGuessArray;  
    }

我想为wordToGuessArray定义一个构造函数。理想情况下,我希望selectWord()方法设置wordToGuessArray参数。但是,在我运行此方法之后,我才知道变量数组的长度,并且我无法在不明确定义char[x]的情况下创建构造函数。 This question的数组长度设置为main。我不能因为任务中的要求限制。那么为了正确地selectWord()我的变量,这个set方法需要在哪里生存?

我很难想象出可行的程序流程。

2 个答案:

答案 0 :(得分:1)

您可能正在寻找param关键字。它允许您将不常见的参数数量传递给构造函数,如下例所示:

public static void UseParams2(params object[] list) 
{
   for ( int i = 0 ; i < list.Length ; i++ )
      Console.WriteLine(list[i]);
   Console.WriteLine();
}

发布到您的帖子,您的代码可能如下所示:

public char[] selectWord(params string[] hangmanWords)
{
    Random wordIndex = new Random();
    int randomIndex = wordIndex.Next(0, hangmanWords.Count()); 
    wordToGuess = hangmanWords[randomIndex].ToUpper();
    char[] wordToGuessArray = wordToGuess.ToCharArray();                                         // convert word to array of letters
    return wordToGuessArray;  
}

答案 1 :(得分:1)

我觉得你有些困惑。 char[] wordToGuessArray = wordToGuess.ToCharArray();不仅为您分配了正确的大小数组,而且还填充了wordToGuess的内容。我有两个建议的更改,以使您的代码更好。

public char[] selectWord()
{
    // get rid of size in this, when you do static init you don't need it
    String[] hangmanWords = new String[] { "time", "person", "year", "way", "day", "thing", "man", "world", "life", "hand", "part", "child", "eye", "woman", "place", "work", "week", "case", "point", "government", "company", "number", "group", "problem", "fact", "good", "new", "first", "last", "long", "great", "little", "own", "other", "old", "right", "big", "high", "different", "small", "large", "next", "early", "young", "important", "few", "public", "bad", "same", "able" };
    // combinme those bottom 5 or so lines into 1.
    return hangmanWords[new Random.Next(0, 49)].ToUpper().ToCharArray();
}

此方法将返回一个字符数组,该数组具有hangmanWords数组中随机选择的字符。我相信这就是你想要的,对吧?

如果你想让字符数组wordToGuessArray成为你的类的属性,那么只需将它声明为public char[] wordsToGuessArray { get; set; }并使方法为void,将return替换为wordsToGuessArray。

示例构造函数;

 // field declared in my class, call it game
 public char[] wordsToGuessArray { get; set; }

 public Game(char[] word)
 {
       wordToGuessArray = word;
 }
 // call it like this
 Game game = new Game(selectWord());

如果您想在构造函数中设置wordToGuessArray,可以在同一个类中设置selectWord() public static方法,在这种情况下,您可以将其称为Game.selectWord()

相关问题