【问题标题】:Stuck on declaring understanding public char in array, C# [duplicate]坚持声明理解数组中的公共字符,C# [重复]
【发布时间】:2016-02-01 19:16:04
【问题描述】:

感谢您抽出宝贵时间查看此内容。

我收到三个错误 一个是我的括号符号围绕声明元音(无效标记) 另外两个是在上下文中存在的汇编语言和元音。我在网站上做了一些挖掘;我觉得我的宣言很接近了。 我找不到任何具体指出问题的东西。 谢谢

    namespace Yhasfeelings
{
public partial class Form1 : Form
{
    public char {vowel {'a', 'e', 'i', 'o', 'u', 'y'};
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        var programVowelGuess = new vowel();
        int count = 0;
        string wordEntry = (textBox1.Text).ToLower();
        for (int i = 0; i < wordEntry.Length; i++)
        { if (textBox1.Text.Contains(programVowelGuess[i]))
            { count++;
            }
        }
        var vowelCount = Convert.ToString(count);
        label1.Text = (vowelCount);
    }
}}

【问题讨论】:

  • 错误是什么,在哪一行?

标签: c# arrays char


【解决方案1】:

我想这就是你要找的东西:

public partial class Form1 : Form
{
    public char[] vowel = new char[] {'a', 'e', 'i', 'o', 'u', 'y'};
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //var programVowelGuess = new vowel();
        int count = 0;
        string wordEntry = textBox1.Text.ToLower();
        for (int i = 0; i < wordEntry.Length; i++)
        {
            if (vowel.Contains(wordEntry[i]))
            {
                count++;
            }
        }
        label1.Text = count.ToString();
    }
}

【讨论】:

  • 大写元音仍然有问题。
  • 谢谢!我可以看到这是一种更有效的设置变量的方法。这种方法似乎更顺畅。
  • 你是对的,史蒂夫,我的错,刚刚编辑以正确处理大写。
  • 也感谢@Steve 的帮助!
【解决方案2】:

更正您的代码以使其可编译:

public char[] vowel = {'a', 'e', 'i', 'o', 'u', 'y'};

private void button1_Click(object sender, EventArgs e)
{
    var programVowelGuess = vowel;
    int count = 0;
    string wordEntry = (textBox1.Text).ToLower();
    for (int i = 0; i < wordEntry.Length; i++)
    { if (programVowelGuess.Contains(wordEntry[i]))
        { count++;
        }
    }
    var vowelCount = Convert.ToString(count);
    label1.Text = vowelCount;
}

【讨论】:

  • 索引超出范围,textbox.text.length > 6?
  • 非常感谢!我知道我是如何错误地声明 char[] 的。
  • @Steve 完全正确...也许我应该更正它...
猜你喜欢
  • 2013-08-09
  • 2011-04-11
  • 2019-10-30
  • 1970-01-01
  • 2011-10-05
  • 2014-01-03
  • 1970-01-01
  • 2022-01-25
  • 2014-07-17
相关资源
最近更新 更多