我尝试使用结构时在c#中无法理解错误

时间:2015-04-14 23:18:49

标签: c# struct

这是我得到的错误

  

错误1'System.Array'不包含'guess_word'的定义,并且没有扩展方法'guess_word'接受类型'System.Array'的第一个参数可以找到(你是否缺少using指令或程序集参考?)C:\ Hangman \ Jers Hangman Game \ Jers Hangman Game \ Form1.cs 183 25 Jers Hangman Game

这是我的一些代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Jers_Hangman_Game
{

public partial class Form1 : Form
{
    int rndNum = 0;
    int wordLength = 0;
    int numGuesses = 0;
    String tempWordLength = "";
    String astericks = "";
    int indexOfGuess;



    public struct Hangman_words
    {
        public int no_letters;
        public string guess_word, hint;
    }

    public Hangman_words[] myWords = new Hangman_words[19];

    public Form1()
    {
        InitializeComponent();
    }

    public void Form1_Load(object sender, EventArgs e)
    {

        myWords[0].no_letters = 5;
        myWords[0].guess_word = "earth";
        myWords[0].hint = "We live on it";

        myWords[1].no_letters = 6;
        myWords[1].guess_word = "garage";
        myWords[1].hint = "store car in it";

        myWords[2].no_letters = 11;
        myWords[2].guess_word = "grandmother";
        myWords[2].hint = "your mothers mother";

        myWords[3].no_letters = 7;
        myWords[3].guess_word = "january";
        myWords[3].hint = "The first month of the year";

        myWords[4].no_letters = 6;
        myWords[4].guess_word = "monkey";
        myWords[4].hint = "Swings from a tree";

        myWords[5].no_letters = 9;
        myWords[5].guess_word = "policeman";
        myWords[5].hint = "called a guard in ireland";

        myWords[6].no_letters = 6;
        myWords[6].guess_word = "recall";
        myWords[6].hint = "to remember";

        myWords[7].no_letters = 3;
        myWords[7].guess_word = "toy";
        myWords[7].hint = "something a child plays with";

        myWords[8].no_letters = 7;
        myWords[8].guess_word = "tobacco";
        myWords[8].hint = "smoking it gives you cancer";

        myWords[9].no_letters = 5;
        myWords[9].guess_word = "thumb";
        myWords[9].hint = "you have one on each hand";

        myWords[10].no_letters = 5;
        myWords[10].guess_word = "stiff";
        myWords[10].hint = "hard to bend";

        myWords[11].no_letters = 8;
        myWords[11].guess_word = "simplest";
        myWords[11].hint = "the most simple";

        myWords[12].no_letters = 5;
        myWords[12].guess_word = "shout";
        myWords[12].hint = "to speak very loudy";

        myWords[13].no_letters = 9;
        myWords[13].guess_word = "selection";
        myWords[13].hint = "making a choice";

        myWords[14].no_letters = 6;
        myWords[14].guess_word = "scared";
        myWords[14].hint = "To be afraid";

        myWords[15].no_letters = 6;
        myWords[15].guess_word = "palace";
        myWords[15].hint = "Where a price lives";

        myWords[16].no_letters = 5;
        myWords[16].guess_word = "adult";
        myWords[16].hint = "One who is legally allowed to vote/drink";

        myWords[17].no_letters = 11;
        myWords[17].guess_word = "explanation";
        myWords[17].hint = "a good answer";

        myWords[18].no_letters = 4;
        myWords[18].guess_word = "calm";
        myWords[18].hint = "To be at ease";

        myWords[19].no_letters = 4;
        myWords[19].guess_word = "kids";
        myWords[19].hint = "children";


    }

    private void button1_Click(object sender, EventArgs e)
    {
        wordHint.Text = myWords[randomNumGenerator()].hint;
        tempWordLength = myWords[randomNumGenerator()].guess_word;
        wordLength = tempWordLength.Length;
        astericksBox.Text = asterickGenerator(tempWordLength);
        astericks = "";
        guesses.Text = numGuesses.ToString();
    }



    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void wordHint_TextChanged(object sender, EventArgs e)
    {

    }

    private void ResetWord_Click(object sender, EventArgs e)
    {
        wordHint.Clear();
        astericksBox.Clear();
        wordHint.Text = myWords[randomNumGenerator()].hint;
        tempWordLength = myWords[randomNumGenerator()].guess_word;
        wordLength = tempWordLength.Length;
        astericksBox.Text = asterickGenerator(tempWordLength);
        astericks = "";
    }

    private int randomNumGenerator() {
        // random number generator
        Random randomNum = new Random();
        rndNum = randomNum.Next(0,19);
        return rndNum;
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {


    }

    private String asterickGenerator(String guess) {
        for (int i = 0; i < wordLength; i++) {
            astericks += "*";
        }
        return astericks;
    }

    private void charGuess( char letter ) {
        if (myWords.guess_word.Contains(letter)) {
        numGuesses+=1   

        }

    }

错误发生在最后一个charGuess方法中。

1 个答案:

答案 0 :(得分:5)

您在上一个方法中没有使用索引器,因此您尝试(如消息所述)调用阵列本身的方法,而不是数组的成员。

myWords[0].guess_word正在访问数组中第0个元素的guess_word字段。 myWords.guess_word正试图访问阵列本身不存在的guess_word字段。

编辑: 除了@Peyman指出的错误,并扩展@ pm100评论,您需要选择您猜测的单词并将其存储为字段。 在您的方法button1_Click中,您要从数组中选择一个单词,但不要存储它。 另外,因为你两次调用randomNumGenerator(),你的提示和字长很可能来自数组中的不同元素。

进一步编辑: 如果允许我的话... 创建一个方法来选择一个单词并调用它,而不是在2个事件处理程序中复制类似的工作。

private void SelectNewWord()
{
    wordHint.Clear();
    astericksBox.Clear();
    // word = new field of type Hangman_words in class.
    // random = new static field of type Random in class.
    word = myWords[random.Next(0, myWords.Length)];
    wordHint.Text = word.hint;
    astericksBox.Text = new string('*', word.quess_word.Length);
}