我该如何解决此代码陷入的无限循环?

时间:2019-01-23 10:09:17

标签: c# arrays sorting bubble-sort

使用方法制作允许您选择其他搜索/排序方法的程序。对于冒泡排序,当我尝试在代码底部输出数组时,将显示“ System.Int32 []”。另外,该代码永远不会真正结束,它只会打印出“ System.Int32 []”和“结束传递_”。我如何阻止这种情况的发生? 谢谢

我尝试将{0}换成实际的变量名,并在for循环中更改'b <_'的值。

        int pass_count = 0;
        bool sorted = false;
        int[] changing_array = new int[5];
        int[] final_array = new int[5];
        int[] starting_array = new int[5];
        for (int a = 0; a < 5; a++)
        {
            Console.WriteLine("Input number {0}", (a + 1));
            changing_array[a] = Convert.ToInt32(Console.ReadLine());
        }
        Array.Copy(changing_array, final_array, 5);
        Array.Copy(changing_array, starting_array, 5);
        Array.Sort(final_array);



        while (sorted == false)
        {
            for (int b = 0; b < 4; b++)
            {
                int c = b++;
                int temp;
                if (changing_array[b] > changing_array[c])
                {
                    temp = changing_array[b];
                    changing_array[b] = changing_array[c];
                    changing_array[c] = temp;
                }
                else
                {
                    continue;
                }
            }
            pass_count++;
            if (changing_array == final_array)
            {
                Console.WriteLine("It took {0} passes to sort \n{1} \ninto \n{2} ",pass_count,starting_array,final_array);
                sorted = true;
            }
            else
            {
                Console.WriteLine("End of pass {0}. \n{1} \nis now \n{2} ",pass_count,starting_array,changing_array);
            }
        }

假设数字最后是“ 65,34,23,87,30”,我希望它打印“花了{0}遍对\ n'65,34,23,87,30'\ ninto \ n'23,30,34,65,87'“,但是它只打印'System.Int32 []'和'通行证_'。

4 个答案:

答案 0 :(得分:1)

您的代码中有几个问题。

for循环中,您正在做

int c = b++;

但是没有按照您的意愿。它首先将b的值分配给c,然后将b加1。这意味着,除了forc的值外,这还增加了b循环变量,因此您实际上跳过迭代现在被“交换”了。

相反,您需要这样做:

int c = b+1;

这将c分配为比b大1,而使b保持不变。

最后,您将比较两个数组实例:

if (changing_array == final_array)

这将不起作用。数组是C#中的引用类型,它只是检查两个变量是否指向内存中的相同位置(不是)。相反,您可能需要SequenceEqual

if (changing_array.SequenceEqual(final_array))

此方法一个接一个地比较两个数组中的项,并且仅当它们具有相同的内容时才返回true。

这些更改之后,while循环结束。

对于输出,不能直接将数组传递给Console.WriteLine。此方法只能处理简单的数据类型,并且不知道如何输出数组,因此只能写出类型名称。相反,您应该将数组变成整数。最简单的解决方案是string.Join方法:

string.Join(",",starting_array)

这将创建一个字符串,其中包含用逗号分隔的数组项。

完成这些更改后,代码的完整版本:

int pass_count = 0;
bool sorted = false;
int[] changing_array = new int[5];
int[] final_array = new int[5];
int[] starting_array = new int[5];
for (int a = 0; a < 5; a++)
{
    Console.WriteLine("Input number {0}", (a + 1));
    changing_array[a] = Convert.ToInt32(Console.ReadLine());
}
Array.Copy(changing_array, final_array, 5);
Array.Copy(changing_array, starting_array, 5);
Array.Sort(final_array);

while (!sorted)
{
    for (int b = 0; b < 4; b++)
    {
        int c = b+1;
        int temp;
        if (changing_array[b] > changing_array[c])
        {
            temp = changing_array[b];
            changing_array[b] = changing_array[c];
            changing_array[c] = temp;
        }
        else
        {
            continue;
        }
    }
    pass_count++;
    if (changing_array.SequenceEqual(final_array))
    {
        Console.WriteLine("It took {0} passes to sort \n{1} \ninto \n{2} ", pass_count, string.Join(",",starting_array), string.Join(",", final_array));
        sorted = true;
    }
    else
    {
        Console.WriteLine("End of pass {0}. \n{1} \nis now \n{2} ", pass_count, string.Join(",", starting_array), string.Join(",", final_array));
    }
}

还请注意,我使用的是!solved而不是solved == false。两者是等效的,但第一个版本更常用且更简洁。

答案 1 :(得分:0)

使用string.Join方法:

string.Join(",", yourArray)

...而不是尝试输出数组对象本身。

答案 2 :(得分:0)

进入无限循环的原因是,如果在排序== false循环内,应用程序中的控件将移至其他部分。在这个其他部分中,排序后的部分仍然为假,因此循环继续进行。

因此,您必须如下所示在循环的else部分中`const SettingsStack = createStackNavigator({ Settings: { screen: SettingsScreen } }, { navigationOptions: { header: null } })` `const MoreStack = createStackNavigator({ More: { screen: MoreScreen } }, { navigationOptions: { header: null } })` `const MainStack = creatSwitchNavigatore({ SettingsStack : SettingsStack, MoreStack : MoreStack }) 退出循环,以通过使用下面的代码所示的break语句来防止无限循环。另外,除了使用break语句,您还可以设置break,这也可以防止无限循环。

sorted =  true;

答案 3 :(得分:0)

通过代码并添加一些注释可能是最简单的:

    int pass_count = 0;
    bool sorted = false;
    int[] changing_array = new int[5];
    int[] final_array = new int[5];
    int[] starting_array = new int[5];
    for (int a = 0; a < 5; a++)
    {
        Console.WriteLine("Input number {0}", (a + 1));
        changing_array[a] = Convert.ToInt32(Console.ReadLine());
    }
    Array.Copy(changing_array, final_array, 5);
    Array.Copy(changing_array, starting_array, 5);
    Array.Sort(final_array);



    while (sorted == false)
    {
        //you should consider having a bool here to track whether a change was made to the array
        //if the for loop runs without making a change, the array is sorted
        for (int b = 0; b < 4; b++) //don't use 4, use changing_array.Length - 1
        {
            int c = b++; //i'd avoid this, because it's potentially confusing as to what b and c values are after it runs. remove use of C and just use b+1 instead
            int temp; //move this into the if, it doesn't need to be out here
            if (changing_array[b] > changing_array[c])
            {
                temp = changing_array[b];
                changing_array[b] = changing_array[c];
                changing_array[c] = temp;
                //you made a change, so set your changeMade bool to true here
            }
            else //this else is totally redundant - the only thing it instructs is to continue looping, which will happen anyway
            {
                continue;
            }
        }
        //test your changeMade Boolean here - if it is false, then set sorted = true
        pass_count++;

        //this isn't how you compare if all the elements of array 1 are in the same order as array 2
        //this is how you compare whether two array variables refer to the same object in memory
        //they don't, so this is always false
        if (changing_array == final_array)
        {
            //this prints Int32[] because when you pass an object into something that
            //needs a string, the runtime calls ToString() on it to make it into a string
            //ToString() isn't anything special for an array, it doesn't print the elements
            //so the default object.ToString() is used, which just prints the type of the object
            //one way to turn an array into a string representation is to say
            //string.Join(",", starting_array)
            //The join method will visit each element, adding them to a comma separated string
            Console.WriteLine("It took {0} passes to sort \n{1} \ninto \n{2} ",pass_count,starting_array,final_array);
            sorted = true;
        }
        else //the reason your loop runs forever is because this else always runs, and sorted is never made true
        {
            Console.WriteLine("End of pass {0}. \n{1} \nis now \n{2} ",pass_count,starting_array,changing_array);
        }
    }