Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.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尖锐的游戏。超出范围误差_C# - Fatal编程技术网

C# c尖锐的游戏。超出范围误差

C# c尖锐的游戏。超出范围误差,c#,C#,我对c有点陌生,收到一条错误消息。我正在尝试重新创建一个发布在youtube上的程序,网址为: 我似乎遇到了一些问题。它说这是一个超出范围的错误。我一直在尝试不同的方法来解决这个问题,但我似乎什么都做不了 public partial class Form3 : Form { public Form3() { InitializeComponent(); } string word = ""; List<Label> la

我对c有点陌生,收到一条错误消息。我正在尝试重新创建一个发布在youtube上的程序,网址为:

我似乎遇到了一些问题。它说这是一个超出范围的错误。我一直在尝试不同的方法来解决这个问题,但我似乎什么都做不了

public partial class Form3 : Form
{
     public Form3()
     {
        InitializeComponent();
    }
    string word = "";
    List<Label> labels = new List<Label>();
    int amount = 0;

    enum BodyParts
    {
        Head,
        Left_Eye,
        Right_Eye,
        Mouth,
        Right_Arm,
        Left_Arm,
        Body,
        Left_Leg,
        Right_Leg,

    }
    void drawhangpost()
    {
        Graphics g = panel1.CreateGraphics();
        Pen p = new Pen(Color.Brown, 10);
        g.DrawLine(p, new Point(130, 218), new Point(130, 5));
        g.DrawLine(p, new Point(135, 5), new Point(65, 5));
        g.DrawLine(p, new Point(60, 0), new Point(60, 50));
        //DrawBodyPart(BodyParts.Head);//just for show
        //DrawBodyPart(BodyParts.Left_Eye);
        //DrawBodyPart(BodyParts.Right_Eye);
        //DrawBodyPart(BodyParts.Mouth);
        //DrawBodyPart(BodyParts.Right_Arm);
        //DrawBodyPart(BodyParts.Left_Arm);
        //DrawBodyPart(BodyParts.Body);
        //DrawBodyPart(BodyParts.Left_Leg);
        //DrawBodyPart(BodyParts.Right_Leg);
        //MessageBox.Show(GetRandomWord());
    }
    void DrawBodyPart(BodyParts bp)
    {
        Graphics g = panel1.CreateGraphics();
        Pen p = new Pen(Color.Blue, 2);
        if (bp == BodyParts.Head)
            g.DrawEllipse(p, 40, 50, 40, 40);
        else if (bp == BodyParts.Left_Eye)
        {
            SolidBrush s = new SolidBrush(Color.Black);
            g.FillEllipse(s, 50, 60, 5, 5);
        }
        else if (bp == BodyParts.Right_Eye)
        {
            SolidBrush s = new SolidBrush(Color.Black);
            g.FillEllipse(s, 63, 60, 5, 5);
        }
        else if (bp == BodyParts.Mouth)
        {
            g.DrawArc(p, 50, 60, 20, 20, 45, 90);
        }
        else if (bp == BodyParts.Body)
            g.DrawLine(p, new Point(60, 90), new Point(60, 170));
        else if (bp == BodyParts.Left_Arm)
            g.DrawLine(p, new Point(60, 100), new Point(30, 85));
        else if (bp == BodyParts.Right_Arm)
            g.DrawLine(p, new Point(60, 100), new Point(90, 85));
        else if (bp == BodyParts.Left_Leg)
            g.DrawLine(p, new Point(60, 170), new Point(30, 190));
        else if (bp == BodyParts.Right_Leg)
            g.DrawLine(p, new Point(60, 170), new Point(90, 190));
    }
    void MakeLables()
    {
        word = GetRandomWord();
        char[] chars = word.ToCharArray();
        int between = 330 / chars.Length - 1;
        for(int i = 0; i < chars.Length - 1; i++)
        {
            labels.Add(new Label());
            labels[i].Location = new Point((i * between) + 10, 80);
            labels[i].Text = "_";
            labels[i].Parent = groupBox2;
            labels[i].BringToFront();
            labels[i].CreateControl();
        }
        label1.Text = "Word Length: " + (chars.Length - 1).ToString();
    }
    string GetRandomWord()
    {
        WebClient wc = new WebClient();
        string wordList = wc.DownloadString("https://raw.githubusercontent.com/Tom25/Hangman/master/wordlist.txt");
        string[] words = wordList.Split('\n');
        Random ran = new Random();
        return words[ran.Next(0, words.Length - 1)];
    }
    private void form3_shown(object sender, EventArgs e)
    {
        drawhangpost();
        MakeLables();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        char letter = textBox1.Text.ToLower().ToCharArray()[0];
        if (!char.IsLetter(letter))
        {
            MessageBox.Show("You can only submit letters.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
        if (word.Contains(letter))
        {
            char[] letters = word.ToCharArray();
            for (int i = 0; i < letters.Length; i++)
            {
                if (letters[i] == letter)
                    labels[i].Text = letter.ToString();//Line gives out of rage error 
            }
            foreach (Label l in labels)
            if (l.Text == "_") return;
            MessageBox.Show("You have won", "Congrats");
            ResetGame();
        }
        else
        {
            //MessageBox.Show("The letter you guessed is wrong", "Sorry");
            label2.Text += " " + letter.ToString() + ",";
            DrawBodyPart((BodyParts)amount);
            amount++;
            if (amount == 9)
            {
                MessageBox.Show("Sorry but you lost, the word was " + word);
                ResetGame();
            }
        }
    }
    void ResetGame()
    {
        amount = 0;// testing not sure
        Graphics g = panel1.CreateGraphics();
        g.Clear(panel1.BackColor);
        GetRandomWord();
        MakeLables();
        drawhangpost();
        label2.Text = "Missed: ";
        textBox1.Text = "";
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (textBox2.Text == word)
        {
            MessageBox.Show("You Have won", "Congrats");
            ResetGame();
        }
        else
        {
            MessageBox.Show("The word you guest was wrong", "Sorry");
            DrawBodyPart((BodyParts)amount);
            amount++;
            if (amount == 9)
            {
                MessageBox.Show("Sorry but you lost, the word was " + word);
                ResetGame();
            }
        }
    }
}

}我最初的答案完全错了

问题就在这里。标签是类型标签的列表。列表长度在MakeLabels中定义,如下所示:

char[] chars = word.ToCharArray();
...
for(int i = 0; i < chars.Length - 1; i++)
{
        labels.Add(new Label());
        ...
        labels[i].Text = "_";
        ...
所以,列表长度是chars.length-1

但在按钮单击功能中,您有:

        char[] letters = word.ToCharArray();
        for (int i = 0; i < letters.Length; i++)
        {
            if (letters[i] == letter)
                labels[i].Text = letter.ToString();
         ...
在这里,你我可以转到letters.Length,它大于标签列表的长度。如果要将字母分配给标签[i]。与字母[i]索引相同的文本,则标签的数量应与潜在字母的数量相同。这意味着您应该将MakeLabels更改为:

for(int i = 0; i < chars.Length; i++)

制作标签时,缺少最后一个标签:

for(int i = 0; i < chars.Length - 1; i++)
    {
        labels.Add(new Label());
        labels[i].Location = new Point((i * between) + 10, 80);
        labels[i].Text = "_";
        labels[i].Parent = groupBox2;
        labels[i].BringToFront();
        labels[i].CreateControl();
    }

当i小于单词长度的1时,循环停止。如果单词长度为6个字符,标签将停止在索引4处,因为5不小于6-1。将其更改为产生错误的行?标签[i]。Text=letter.ToString;我该如何定义标签实际上,你已经定义了标签——这是一个列表。我被告知有些代码也会产生编译错误,特别是这段代码:if字母[I]==字母我已经重新编写了答案。程序在不修改代码的情况下制作了正确数量的标签。如果我改变它,我不明白这怎么可能是真的。你确定你只做了更改吗?如果我更改了代码,下面的代码为int i=0;i<字符长度-1;i++to表示int i=0;一般来说,你应该把它写成forint i=0;iif (word.Contains(letter)) { char[] letters = word.ToCharArray(); for (int i = 0; i < letters.Length; i++) { if (letters[i] == letter) labels[i].Text = letter.ToString();//Line gives out of rage error } foreach (Label l in labels) if (l.Text == "_") return; MessageBox.Show("You have won", "Congrats"); ResetGame(); }