0

相当新的 C# 开发人员。我正在尝试开发一个疯狂的库生成器。我有一个button_1标有“动词”的按钮,它应该生成一个随机动词。动词来自一个字符串数组,它是一个动词列表。我有另一个button_5标记为“添加新动词”的按钮,它应该将相应文本框中的动词添加到动词数组中。我遇到的问题是它只生成我单击时输入的最后一个动词,button_1标记为“动词”。

代码如下所示:

namespace WindowsFormsApplication1
{
    public class Arrays
    {
        public static string[] verbarray = new string[10];
    }
}

public void button5_Click(object sender, EventArgs e)
{
    for (int iverb = 0; iverb < Arrays.verbarray.Length; iverb++)
    {
        Arrays.verbarray[iverb] = Convert.ToString(this.txtaddverb.Text);
    }
}

public void button1_Click(object sender, EventArgs e)
{
    Random randomverb = new Random();
    verb.Text = Arrays.verbarray[randomverb.Next(0, Arrays.verbarray.Length)];
}
4

5 回答 5

2

Your verb-adding procedure is the problem.

You have set up a loop that iterates across all entries in your verb array, and replaces each one with the current value of your verb textbox. So, when you click the button to select a new verb, you're randomly selecting a verb from a list of entries that's always going to be identical once you've started adding verbs - and the verb chosen will always be the last one you added.

I'd suggest you take a look at a List, which can grow more readily and simplifies your Add problem. Might help!

// a little pseudocode to help with the notion..wire in your 
// event handlers accordingly
class VerbManager
{

 List<String> verbs= new List<String>();
 Random picker = new Random();

 public void addVerb(String newVerb)
 {
     verbs.Add(newVerb);
 }

 public string pickRandomVerb()
 {
     return verbs[picker.Next(0,verbs.Count)];
 }
}
于 2013-02-27T16:57:20.923 回答
1

在 C# 中,数组的长度是固定的,因此,如果您的应用程序基于能够将动词动态添加到公共动词池中,您应该考虑更改

public static string[] verbarray = new string[10];

public static List<string> verbList = new List<string>();

一个列表的大小可以增长,所以如果你已经有 10 个动词并想添加另一个,那没问题。然后,所需的代码button5_Click将简单地阅读:

verbList.Add(txtaddverb.Text);
于 2013-02-27T17:00:19.187 回答
0

移动:

Random randomverb = new Random(); 

到班级水平。

于 2013-02-27T16:52:20.600 回答
0

这是我在解决所有错误问题后最终使用的代码:

命名空间 WindowsFormsApplication1

{

public class Lists

{
    public static List<string> verbList = new List<string>();

    public static Random randomverb = new Random();
}

}

公共字符串pickRandomVerb()

    {
        return Lists.verbList[Lists.randomverb.Next(0, Lists.verbList.Count)];
    }
    public void button1_Click(object sender, EventArgs e)
    {
        if (Lists.verbList.Count > 0) verb.Text = pickRandomVerb();
    }

公共无效按钮5_Click(对象发送者,EventArgs e)

    {
        Lists.verbList.Add(txtaddverb.Text);
    }
于 2013-02-27T17:53:44.897 回答
-2

你应该在这里更正你的逻辑:

   public void button5_Click(object sender7, EventArgs e)
    {
        for (int iverb = 0; iverb < Arrays.verbarray.Length; iverb++)
        {
            Arrays.verbarray[iverb] = Convert.ToString(this.txtaddverb.Text);
        }
    }

你想要什么

public void button5_Click(object sender7, EventArgs e)
        {
            // suppose index 6
                Arrays.verbarray[6] = Convert.ToString(this.txtaddverb.Text);

        }
于 2013-02-27T16:56:38.343 回答