我目前正在做一项我正在努力的家庭作业。该计划的目标是:
创建一个程序,允许用户记录和查看最多1000个事件的名称。事件的名称将存储在数组中。名称只能按顺序存储和查看:I,e,用户第一次按“Set”存储名称时,它将被放置在数组的索引0中;第二次用户存储名称,它将存储在索引1等中。类似地,当用户第一次按下“查看”时,在用户显示在数组的索引0中找到的项目;第二次用户按下“View”时,会显示在数组的索引1中找到的项目等。
我目前有代码:
namespace ArrayHW
{
public partial class Form1 : Form
{
int[] eventsArray;
const int SIZE = 1000;
public Form1()
{
InitializeComponent();
}
private void btnSet_Click(object sender, EventArgs e)
{
eventsArray = new int[SIZE];
for (int index = 0; index < eventsArray.Length - 1; index++)
{
eventsArray[index] = Convert.ToInt32(txtEventName.Text);
}
}
private void btnView_Click(object sender, EventArgs e)
{
for (int index = 0; index < eventsArray.Length- 1; index++)
{
Debug.WriteLine(eventsArray[index]);
txtName.Text = Convert.ToString(eventsArray[index]);
}
}
}
}
当我运行表单时,我只得到index = 0,1,2,3等的结果或者我刚刚输入到数组中的任何内容
private void btnSet_Click(object sender, EventArgs e)
{
eventsArray = new int[SIZE];
for (int index = 0; index < eventsArray.Length - 1; index++)
{
eventsArray[index] = Convert.ToInt32(txtEventName.Text);
}
}
而不是像它应该的那样按顺序显示。谁能告诉我一个更好的方法来解决这个问题,或者帮助我找出我做错了什么?非常感谢你。
答案 0 :(得分:2)
请阅读代码块中的注释。希望这能帮助您解决问题。
public partial class Form1 : Form
{
const int SIZE = 1000;
int[] eventsArray = new int[SIZE];
//as per your requirement, you would need these
//to display and set items at proper index in the array.
int _setIndex = 0;
int _viewIndex = 0;
public Form1()
{
InitializeComponent();
}
private void btnSet_Click(object sender, EventArgs e)
{
//this is where your problem is. Every time the Set btn is clicked,
//you are creating a new array. Therefore you are only seeing what you added in the
//last click. Anything that was added to the array on the prior clicks are lost because
//you just created a new array with the line below.
//eventsArray = new int[SIZE];
//You would need to comment the line above because this is code block is being
//executed on every btnSet click. New up this array only once by moving this to global scope.
//for (int index = 0; index < eventsArray.Length - 1; index++)
//{
// eventsArray[index] = Convert.ToInt32(txtEventName.Text);
//}
//Since we have created fields to keep track of _setIndex, all we need to do is:
if (_setIndex < SIZE)
{
eventsArray[_setIndex] = Convert.ToInt32(txtEventName.Text);
_setIndex++;
}
}
private void btnView_Click(object sender, EventArgs e)
{
//this wont be necessary.
//for (int index = 0; index < eventsArray.Length - 1; index++)
//{
// Debug.WriteLine(eventsArray[index]);
// txtName.Text = Convert.ToString(eventsArray[index]);
//}
if(eventsArray.Length > _viewIndex)
{
txtName.Text = Convert.ToString(eventsArray[_viewIndex]);
//this is to fulfill the requirement below:
// Similarly, the first time the user presses “View”, the item found in index 0
// of the array is shown to the user; the second time the user presses “View”,
// the item found in index 1 of the array is shown, etc.
_viewIndex++;
}
}
}
答案 1 :(得分:0)
试试这个:
int[] eventsArray = new int[SIZE];
int index = 0;
const int SIZE = 1000;
private void btnSet_Click(object sender, EventArgs e)
{
eventsArray[index++] = Convert.ToInt32(txtEventName.Text);
}