【问题标题】:Why is my loop mixing the first loop and final loops first input为什么我的循环混合了第一个循环和最终循环的第一个输入
【发布时间】: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 数组的引用!

标签: c# winforms for-loop


【解决方案1】:
 //compares the two numbers and swaps if less than
if (string.Compare(sorter[index], sorter[index + 1]) > 0) 
{
    //swap
    temp = sorter[index];
    sorter[index] = sorter[index + 1];
    sorter[index + 1] = temp;
}

切换到大于。应该将顺序切换到正确的方式。

注意:如果您尝试对整个数组进行排序,而不仅仅是标题,您应该考虑为每组数组索引创建一个对象,然后将它们变成 List&lt;Book&gt;超级简单的排序。

【讨论】:

    【解决方案2】:

    您只是在 titleArray 上调用 bubbleSort,而不是其他数组。

    您的输出是每个数组的一个元素,但您尚未对其他数组进行排序,因此索引不再匹配。

    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("-+-");
    }
    

    您应该将数据存储在List 对象中,而不是四个单独的数组,然后对列表进行排序。这样,数据片段将保持在一起。

    要使这与 4 个单独的数组一起工作,您需要对其他数组进行排序 - 但为每个数组调用 bubbleSort 是不够的,您需要移动它们的项目以匹配 titleArray 中的更改.
    换句话说,bubbleSort 将不得不采用 ALL 数组,查看titleArray 但移动所有数组中的相应项。

    这是一种奇怪的做事方式,我建议联系提出问题的人并要求澄清。

    【讨论】:

    • 我更喜欢 2D 数组,但有问题的 excersize 专门为每个元素请求一个 1D 数组,我有二进制文件的第二部分,它只有已排序的 TitleArrays,但我认为我我对为什么我的循环不起作用感到更加困惑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多