需要帮助使用冒泡法对我的数组进行排序,因为我的文本框显示排序后的数字只显示 0

need help sorting my array using bubble method because my text box to show the sorted numbers only show 0

我创建了一个数组,当您单击按钮时它会接收随机数,并且一切正常。然后我有一个按钮可以使用冒泡排序对数字进行排序并将其显示在另一个 textbox 中,但是当我单击排序按钮时,我得到的只是重复的 0。我需要帮助解决我的错误。

namespace RandomArray
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within    a       Frame.
    /// </summary>
    public sealed partial class Arraytwenty : Page
    {
        public Arraytwenty()
        {
            this.InitializeComponent();
        }
        int[] arraytwenty = new int[20];
        public void GenArray20_Click(object sender, RoutedEventArgs e)
        {
            int[] arraytwenty = new int[20];
            Random Number = new Random();

            for (int i = 0; i < 20; i++)
            {
                int randomnum = Number.Next(1, 101);
                arraytwenty[i] = randomnum;

                Original20Array.Text = Original20Array.Text + " " + Convert.ToString(arraytwenty[i]);
            }




        }

        private void bubsortbtn_Click(object sender, RoutedEventArgs e)
        {
            int temp = 0;
            for (int i = 0; i < arraytwenty.Length; i++)
            {
                for (int j = 0; j < arraytwenty.Length - 1; j++)
                {
                    if (arraytwenty[j] > arraytwenty[j + 1])
                    {
                        temp = arraytwenty[j + 1];
                        arraytwenty[j + 1] = arraytwenty[j];
                        arraytwenty[j] = temp;
                    }
                }
            }
            string value = " ";
            for (int i = 0; i < arraytwenty.Length; i++)
            {
                value += arraytwenty[i].ToString() + " ";


            }

            bubblesortTextbox.Text = value;

        }

        private void clrArraytwenty_Click(object sender, RoutedEventArgs e)
        {
            Original20Array.Text = String.Empty;
            bubblesortTextbox.Text = String.Empty;
        }
    }
}

您在 GenArray20_Click 中对数组进行了本地初始化,因此它使用本地副本来填充数据

请尝试更新后的代码。

请更新此部分

    public void GenArray20_Click(object sender, RoutedEventArgs e)
    {
        // You have local initilization of array
        // so code inside this method will load data into this array instead of global
        //int[] arraytwenty = new int[20];
        Random Number = new Random();

        for (int i = 0; i < 20; i++)
        {
            int randomnum = Number.Next(1, 101);
            arraytwenty[i] = randomnum;

            Original20Array.Text = Original20Array.Text + " " + Convert.ToString(arraytwenty[i]);
        }
    }

不要对变量使用相同的名称,如您所知,这可能会导致意外行为:

namespace RandomArray
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within    a       Frame.
    /// </summary>
    public sealed partial class Arraytwenty : Page
    {
        //Declare your class array for storing integers, this will have scope in any method of your class
        private int[] arraytwenty;

        public Arraytwenty()
        {
            //initialize your array
            this.arraytwenty = new int[20];
            this.InitializeComponent();
        }

        public void GenArray20_Click(object sender, RoutedEventArgs e)
        {
            //Don't create a local array when you want to use it in your other class methods
            //int[] arraytwenty = new int[20];
            Random Number = new Random();

            for (int i = 0; i < 20; i++)
            {
                int randomnum = Number.Next(1, 101);
                arraytwenty[i] = randomnum;

                Original20Array.Text = Original20Array.Text + " " + Convert.ToString(arraytwenty[i]);
            }




        }

        private void bubsortbtn_Click(object sender, RoutedEventArgs e)
        {
            int temp = 0;
            for (int i = 0; i < arraytwenty.Length; i++)
            {
                for (int j = 0; j < arraytwenty.Length - 1; j++)
                {
                    if (arraytwenty[j] > arraytwenty[j + 1])
                    {
                        temp = arraytwenty[j + 1];
                        arraytwenty[j + 1] = arraytwenty[j];
                        arraytwenty[j] = temp;
                    }
                }
            }
            string value = " ";
            for (int i = 0; i < arraytwenty.Length; i++)
            {
                value += arraytwenty[i].ToString() + " ";


            }

            bubblesortTextbox.Text = value;

        }

        private void clrArraytwenty_Click(object sender, RoutedEventArgs e)
        {
            Original20Array.Text = String.Empty;
            bubblesortTextbox.Text = String.Empty;
        }
    }
}