添加char到char数组问题,hangman游戏

时间:2013-03-13 20:37:08

标签: c# console

问题是我发现我的方法需要构建一个char数组 - >来自多个char变量的char []。有人能指出我正确的方向吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
    string[] wordList = { 
                            "Baseball", "Tackle", "Dubstep", "Ignorance", "Limitation", "Sausage",
                            "Destruction", "Patriot", "Computing", "Assembly", "Coding", "Hackers",
                            "Football", "Downward"
                        };
    static void Main(string[] args)
    {
        int guessRemain = 7;
        int wordSel = GenerateRandom();
        Program o = new Program();


        char[] wordChar = o.wordList[wordSel].ToLower().ToCharArray();
        int MAX_BUF = wordChar.Length;


        Console.WriteLine("\nHANGMAN v 1.0\n\n\n\n");
        char[] userInput = PromptUserEntry();
        char[] solution = ScanForMatchingLetter(wordChar, MAX_BUF, userInput);


        Console.Read();
    }
    private static char ScanForMatchingLetter(char[] wordChar, int MAX_BUF, char[] userInput)
    {
        char[] solution = new char[MAX_BUF];
        for (int i = 0; i < MAX_BUF; ++i)
        {
            if (userInput[0] == wordChar[i])
            {
                solution[i] = userInput[0];

            }


        }
        return solution;
    }
    private static char[] PromptUserEntry()
    {
        Console.WriteLine("Pick a letter:");
        char[] userInput = Console.ReadLine().ToCharArray();
        return userInput;
    }
    private static void DisplayGuessLetterLine(char[] solution)
    {

        Console.Write(solution);

    }
    private static int GenerateRandom()
    {
        Random randNum = new Random();
        int wordSel = randNum.Next(0, 13);
        return wordSel;
    }

}
}

我这里有一个返回类型的问题;返回类型被指定为char但我返回一个char []。

1 个答案:

答案 0 :(得分:1)

在使用char数组的每个实例中,将其替换为

List<char>

列表允许您随意添加和删除,为您重新装箱底层数组,以便您不必担心它。

我已用你的决议更新了我的答案。当您只有一个用户输入时,使用List而不是char数组并仅返回单个字符,而不返回数组。我希望这有助于解决您的问题

class Program
{
    readonly string[] wordList = { 
                        "Baseball", "Tackle", "Dubstep", "Ignorance", "Limitation", "Sausage",
                        "Destruction", "Patriot", "Computing", "Assembly", "Coding", "Hackers",
                        "Football", "Downward"
                    };
    static void Main(string[] args)
    {
        int guessRemain = 7;
        int wordSel = GenerateRandom();
        Program o = new Program();


        List<char> wordChar = o.wordList[wordSel].ToLower().ToList();
        int MAX_BUF = wordChar.Count;


        Console.WriteLine("\nHANGMAN v 1.0\n\n\n\n");
        char userInput = PromptUserEntry();
        List<char> solution = ScanForMatchingLetter(wordChar, MAX_BUF, userInput);


        Console.Read();
    }
    private static List<char> ScanForMatchingLetter(List<char> wordChar, int MAX_BUF, char userInput)
    {
        List<char> solution = new char[MAX_BUF].ToList();
        for (int i = 0; i < MAX_BUF; ++i)
        {
            if (userInput == wordChar[i])
            {
                solution[i] = userInput;

            }


        }
        return solution;
    }
    private static char PromptUserEntry()
    {
        Console.WriteLine("Pick a letter:");
        char userInput = Console.ReadLine()[0];
        return userInput;
    }
    private static void DisplayGuessLetterLine(List<char> solution)
    {

        Console.Write(solution);

    }
    private static int GenerateRandom()
    {
        Random randNum = new Random();
        int wordSel = randNum.Next(0, 13);
        return wordSel;
    }
}