0

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

    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]这个问题在 main.xml 中设置了数组长度。由于任务中的要求限制,我的不能。那么这种selectWord()方法需要在哪里才能正确使用set我的变量?

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

4

2 回答 2

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;  
}
于 2013-05-10T23:38:24.373 回答
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; }并使方法无效,将返回替换为对 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()

于 2013-05-10T23:39:49.030 回答