【发布时间】:2018-10-18 13:20:18
【问题描述】:
基本上,我有 4 个数组,其中包含我在 UI 中输入的值。
我正在尝试使用 for 循环将我的数组写入二进制文件,并且数组可以容纳 10 个元素,第一个循环始终具有最终循环的一个元素,反之亦然:
循环由-+-分隔,第一个应该全是“a”,最后一个应该全是“c”。
我在这里做错了什么?
这是我的主要内容:
private void saveButton_Click(object sender, EventArgs e)
{
//quick if to make sure that there's actually some substance in the input fields
if (titleBox.Text == String.Empty || platformBox.Text == String.Empty ||
genreBox.Text == String.Empty || publisherBox.Text == String.Empty)
{
MessageBox.Show("Error:\n\nPlease Input something into each the fields required","Wow, the audacity of this user");
}
else
{
game.Title = titleBox.Text;
game.Platform = platformBox.Text;
game.Genre = genreBox.Text;
game.Publisher = publisherBox.Text;
//Try catch implemented to stop and alert the user that the arrays have hit capacity
try
{
//continued appending of the 1D arrays
for (arrEnd = 0; arrEnd < 1; arrEnd++)
{
titleArray[arrIterator] = game.Title;
platformArray[arrIterator] = game.Platform;
genreArray[arrIterator] = game.Genre;
publisherArray[arrIterator] = game.Publisher;
}
if (firstEntry == true)
{
displayBox.Items.Add("-------------");
firstEntry = false;
}
//taking what had been stowed in the arrays and placing it in the listbox
displayBox.Items.Add(titleArray[arrIterator]);
displayBox.Items.Add(platformArray[arrIterator]);
displayBox.Items.Add(genreArray[arrIterator]);
displayBox.Items.Add(publisherArray[arrIterator]);
displayBox.Items.Add("-------------");
titleBox.Focus();
bubbleSort(titleArray);
//creating a duplicate Title array to be sorted
sortedTitleArray = titleArray;
//Attempting to generate a data file with the info inside
try
{
//the creating and subsequent writing of the binary file
string fileName = "Books";
StreamWriter sw = new StreamWriter(fileName);
for (arrData = 0; arrData < titleArrayL; arrData++ )
{
sw.WriteLine("-+-");
sw.WriteLine(titleArray[arrData]);
sw.WriteLine(platformArray[arrData]);
sw.WriteLine(genreArray[arrData]);
sw.WriteLine(publisherArray[arrData]);
sw.WriteLine("-+-");
}
sw.WriteLine("------------------");
for (arrData = 0; arrData < titleArrayL; arrData++)
{
sw.WriteLine(sortedTitleArray[arrData]);
}
titleArrayL++;
sw.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
MessageBox.Show("Didn't work out, sorry Homie\nReason Being, none of the available or predetermined paths for the data file were available");
}
//Using the Bubble sort method to sort the Title array
bubbleSort(sortedTitleArray);
}
catch
{
MessageBox.Show("Arrays have hit capacity and can hold no more values","Sorry dawg");
saveButton.Enabled = false;
}
/*stopping the program from adding more to the arrays than they can specifically handle, disabled due to try catch above, might reactivate though since cleaner
if(arrIterator == 9)
{
saveButton.Enabled = false;
}
*/
arrEnd = 0;
arrIterator++;
dumpeet();
}
}
还使用了定制的Bubble Sort:
void bubbleSort(string[] sorter)
{
string temp;
for (int write = 0; write < sorter.Length; write++)
{
for (int index = 0; index < (sorter.Length - 1); index++)
{
if (string.Compare(sorter[index], sorter[index + 1]) < 0) //compares the two numbers and swaps if less than
{
//swap
temp = sorter[index];
sorter[index] = sorter[index + 1];
sorter[index + 1] = temp;
}
}
}
}
【问题讨论】:
-
你能断点并检查你循环中的值吗?也许问题出在数组的内容上,而不是循环本身。
-
让我们看看你的冒泡排序方法
-
我相信这是您对
titles进行排序的方式,因为它们是混合在一起的。请发布冒泡排序 -
抱歉忘记添加冒泡排序,现在有
-
这一行:
sortedTitleArray = titleArray;不会像您的评论指出的那样创建重复的数组。它创建另一个对 same 数组的引用!