C#的初学者,如何使用用户输入来填充数组 [英] beginner in C#, how to fill an array using user input

查看:75
本文介绍了C#的初学者,如何使用用户输入来填充数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,我正尝试根据用户输入创建一个数组.我真的不在乎它是什么类型(双精度,整数等)或多长时间([10],[20]等).我如何制作一个数组,当按下按钮时,该数组从文本框中获取输入,然后将其存储在[0]中,然后存储文本框中的下一个用户输入,并将其存储在[1]中,依此类推,直到数组长度为到达?在此先感谢您,我一直在寻找没有运气的例子. ;)

[添加]
非常感谢!响应速度非常快.我想添加一些错误控制,但是我不确定如何做到最好.现在,我正在尝试使用else-if语句,但是该程序持续的时间太长,即使int超出范围,它也会将int放入列表中!有趣但令人沮丧.代码如下:

[添加]
将其移至此处:否则,如果(分数& gt = 0 =& amp; amp; amp; amp;分数& lt; == 100)
{
ScoreList.Add(score);
int total = 0;
int计数= ScoreList.Count;

现在工作完美!谢谢马克,这对您来说很重要!

[添加]
Gotcha Will.谢谢您的帮助! :)

List<int> ScoreList = new List<int>();

private void Badd_Click(object sender, EventArgs e)
{
    int score = Convert.ToInt32(TBi.Text);

    ScoreList.Add(score);
                     
    if (score <0 | score >100)
    {
        MessageBox.Show ("enter 0-100 only...troublemaker.", "ERROR");               
        TBi.Text = "";
              
    }
    else if (score >=0 && score <=100)
    {
        int total = 0;
        int count = ScoreList.Count;

        for (int s = 0; s < count; s++)
        {
            total += ScoreList[s];
        }
        int average = total / count;
        TBo.Text = (count).ToString();
        TBo2.Text = (average).ToString();
        TBo3.Text = (total).ToString();
    }           
}

private void Bdis_Click(object sender, EventArgs e)
{
    string ScoreListString = "";

    foreach (int score in ScoreList)
       ScoreListString += score.ToString() + "\n";

    MessageBox.Show(ScoreListString, "Sorted Scores");

}

private void Bclr_Click(object sender, EventArgs e)
{
    ScoreList.Clear();
    TBi.Text = "";
    TBo.Text = "";
    TBo2.Text = "";
    TBo3.Text = "";
}</int></int>

解决方案



尝试使用ArrayList.有关ArrayList的更多详细信息,请通过此链接,
http://en.csharp-online.net/ArrayList [


I''m new to C# and I''m trying to make an array out of user input. I really don''t care what type it is (double, int, etc.), or how long it is ([10], [20], etc). How can I make an array that takes input from a textbox when a button is pressed and then stores it in [0], then stores the next user input from the textbox and stores it in [1], etc. until the array length is reached? thanks very much in advance, I''ve been searching for days with no luck finding examples. ;)

[Added]
Thanks a lot! Very fast responses. I want to add some error control, but I''m not sure how best to do it. Right now I''m trying to use an else-if statement but the program continues too long and puts the int in the list even though it''s out of range! Fun but frustrating. Here''s the code:

[Added]
Moved it here: else if (score &gt;=0 &amp;&amp; score &lt;=100)
{
ScoreList.Add(score);
int total = 0;
int count = ScoreList.Count;

Works perfect now! Thanks Marc, that''s a point for you!

[Added]
Gotcha Will. Thanks for the help! :)

List<int> ScoreList = new List<int>();

private void Badd_Click(object sender, EventArgs e)
{
    int score = Convert.ToInt32(TBi.Text);

    ScoreList.Add(score);
                     
    if (score <0 | score >100)
    {
        MessageBox.Show ("enter 0-100 only...troublemaker.", "ERROR");               
        TBi.Text = "";
              
    }
    else if (score >=0 && score <=100)
    {
        int total = 0;
        int count = ScoreList.Count;

        for (int s = 0; s < count; s++)
        {
            total += ScoreList[s];
        }
        int average = total / count;
        TBo.Text = (count).ToString();
        TBo2.Text = (average).ToString();
        TBo3.Text = (total).ToString();
    }           
}

private void Bdis_Click(object sender, EventArgs e)
{
    string ScoreListString = "";

    foreach (int score in ScoreList)
       ScoreListString += score.ToString() + "\n";

    MessageBox.Show(ScoreListString, "Sorted Scores");

}

private void Bclr_Click(object sender, EventArgs e)
{
    ScoreList.Clear();
    TBi.Text = "";
    TBo.Text = "";
    TBo2.Text = "";
    TBo3.Text = "";
}</int></int>

解决方案

Hi,

Try using ArrayList. For more details about ArrayList go through this link,
http://en.csharp-online.net/ArrayList[^]

Regards,
Suresh


create a global arraylist.

for every button click add the textbox.text to arraylist.


In answer to your second question...
That would be because you''re adding the item to your List outside of your if/else if blocks. You need to move the line of code that adds the item to the list into either the "if" block or the "else if" block, depending on which condition should trigger storing the value.


这篇关于C#的初学者,如何使用用户输入来填充数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆