Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 添加字符到字符数组问题,刽子手游戏_C#_Console - Fatal编程技术网

C# 添加字符到字符数组问题,刽子手游戏

C# 添加字符到字符数组问题,刽子手游戏,c#,console,C#,Console,问题是我发现我的方法需要从多个char变量构建char数组->char[]。有人能给我指出正确的方向吗 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { string[] wordList = { "Basebal

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

}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序1
{
班级计划
{
字符串[]字列表={
“棒球”、“铲球”、“Dubstep”、“无知”、“限制”、“香肠”,
“破坏”、“爱国者”、“计算”、“组装”、“编码”、“黑客”,
“足球”,“向下”
};
静态void Main(字符串[]参数)
{
int=7;
int-wordSel=generateradom();
程序o=新程序();
char[]wordChar=o.wordList[wordSel].ToLower().ToCharArray();
int MAX_BUF=wordChar.Length;
Console.WriteLine(“\nHANGMAN v 1.0\n\n\n”);
char[]userInput=PromptUserEntry();
char[]solution=ScanForMatchingLetter(wordChar,MAX_BUF,userInput);
Console.Read();
}
私有静态char ScanForMatchingLetter(char[]wordChar,int MAX\u BUF,char[]userInput)
{
字符[]解决方案=新字符[MAX_BUF];
对于(int i=0;i

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

在使用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;
    }
}
类程序
{
只读字符串[]字列表={
“棒球”、“铲球”、“Dubstep”、“无知”、“限制”、“香肠”,
“破坏”、“爱国者”、“计算”、“组装”、“编码”、“黑客”,
“足球”,“向下”
};
静态void Main(字符串[]参数)
{
int=7;
int-wordSel=generateradom();
程序o=新程序();
List wordChar=o.wordList[wordSel].ToLower().ToList();
int MAX_BUF=wordChar.Count;
Console.WriteLine(“\nHANGMAN v 1.0\n\n\n”);
char userInput=PromptUserEntry();
列表解决方案=ScanForMatchingLetter(wordChar、MAX_BUF、userInput);
Console.Read();
}
私有静态列表ScanForMatchingLetter(列表wordChar、int MAX_BUF、char userInput)
{
列表解决方案=新字符[MAX_BUF].ToList();
对于(int i=0;i
如果需要可变长度存储,请不要使用阵列。
列表
可能更适合您。当您实际需要char[]且方法(
ScanForMatchingLetter
)甚至有char[]时,为什么要返回char?您的所有函数都是类中的静态函数,但word数组是实例变量。您也可以将
static
应用于
wordList
,而不是像现在这样创建程序类的实例。或者,从所有内容中删除
static
,运行一个非静态方法
Run
,然后在
Main
中简单地执行
Program o=new Program();o、 Run()我工作了3到4个小时。。现在完全丢失/更改您的方法
ScanForMatchingLetter
以返回字符[]@UmairAslam请查看我的更新。我想这会对你有很大帮助,但它仍然会给你一些关于转换的错误。但这次它无法将字符串转换为字符。什么是tocharArray()的代码;通用的?因为我想。tolist()不是working@UmairAslam你使用的代码和我发布的代码完全一样吗?它在我的机器上编译正确,运行良好。你能编译它吗?但它不是用机器编译的。我从这里抄的。但是仍然有一些错误,它现在可以正确编译了。但没有给出准确的答案。我的代码是否适用于刽子手游戏?