1

问题是我发现我的方法需要从多个 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[]。

4

1 回答 1

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;
    }
}
于 2013-03-13T20:49:34.863 回答