C# 气泡排序Visual Studio
我无法让程序正确打印数字并正确排序。我需要帮助使程序正常运行。这个项目的最后期限是今晚12点。请帮忙C# 气泡排序Visual Studio,c#,visual-studio,sorting,console,bubble-sort,C#,Visual Studio,Sorting,Console,Bubble Sort,我无法让程序正确打印数字并正确排序。我需要帮助使程序正常运行。这个项目的最后期限是今晚12点。请帮忙 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Sort { class Program { static void Main(string[] args)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sort
{
class Program
{
static void Main(string[] args)
{
List<int> ints = new List<int>() { };
int sort = 0;
for (int i = 1; i > 0; i++)
{
Console.WriteLine("Enter a number");
int number = Convert.ToInt32(Console.ReadLine());
if (number == -1)
break;
{
for (int j = 0; j > ints.Count; j++)
{
if (ints[i] < ints[j])
{
sort = ints[j];
ints[j] = ints[i];
ints[i] = sort;
Console.WriteLine(ints[i].ToString());
}
}
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间排序
{
班级计划
{
静态void Main(字符串[]参数)
{
List ints=new List(){};
int-sort=0;
对于(int i=1;i>0;i++)
{
Console.WriteLine(“输入一个数字”);
int number=Convert.ToInt32(Console.ReadLine());
如果(数字==-1)
打破
{
对于(int j=0;j>int.Count;j++)
{
if(整数[i]<整数[j])
{
排序=整数[j];
ints[j]=ints[i];
ints[i]=排序;
Console.WriteLine(ints[i].ToString());
}
}
}
}
}
}
}
如果您想用C语言对列表进行排序,可以使用内置的list方法:
List<int> list = new List();
// add some elements
list.Sort();
List List=新列表();
//添加一些元素
list.Sort();
最好是问一个关于问题性质的具体问题,以及您希望解决的问题
但是看看你的代码,我看到了一些奇怪的行为。似乎您正在滥用一个循环来进行一个又一个的循环,并向用户索要一个数字。如果数字不是-1
则在一个空白列表上循环,如果内循环的索引值大于外循环,则在交换后打印每一行
如果输入为-1,则中断循环并结束程序
你的问题是,你似乎从来没有任何数字排序?是因为从i==1开始,而数组的索引从0开始,所以排序得到的索引超出了界限吗?是不是你在写数字时没有先完成排序?所有这些
最好是查找一些解释冒泡排序流的伪代码,然后自己实现。如果不能做到这一点,您可以通过简单的互联网搜索轻松找到冒泡排序的C#实现
但为了给你一个跳跃式的开始,我猜你是想:
while(true)
提示用户输入号码
如果是数字!=-1将数字存储到数组中
重复此操作,直到数字==-1,这将中断while循环
基于气泡排序对列表或数组的元素进行排序
循环排序的元素并打印出来
编辑:既然你要求帮助如何修复你的程序,我会给你下面未经测试的代码让你开始。您需要根据“工作”说明实现气泡排序
static void Main(string[] args)
{
List<int> ints = new List<int>();
//capture numbers from user input
while(true)
{
Console.WriteLine("Enter a number");
int number = Convert.ToInt32(Console.ReadLine());
if (number == -1) //If user enters magic number, we break out of while loop
break;
ints.Add(number); //Unless we've broken out, add the number to the list
}
//do your bubble sort here
//this is up to you to implement!
//print the results
foreach(int sortedNumber in ints)
{
Console.WriteLine(sortedNumber);
}
}
static void Main(字符串[]args)
{
List ints=新列表();
//从用户输入捕获数字
while(true)
{
Console.WriteLine(“输入一个数字”);
int number=Convert.ToInt32(Console.ReadLine());
if(number==-1)//若用户输入了幻数,我们就跳出while循环
打破
ints.Add(number);//除非我们已中断,否则请将该数字添加到列表中
}
//你在这里做泡泡排序吗
//这取决于你去实施!
//打印结果
foreach(整数分类整数)
{
控制台写入线(分拣编号);
}
}
请发布一段有用的代码。在您的示例中,{
和}
甚至不平衡。请正确缩进代码。一个好方法是使用VisualStudio的源代码格式化程序。哦,如果是为了这份工作:其他人已经知道了如何排序。搜索“C#排序列表”。哦,你的编程语言是C语言,而不是C++。你可以很容易地在网上找到排序算法。Bing it.我建议关闭这篇文章,因为它没有显示出对当前问题的理解,也没有包含关于预期和实际行为的信息。请参阅@RolandIllig这里是完整的代码。标题和标签表明他们特别想要使用冒泡排序。List.Sort
方法可能会使用其他排序方法,具体取决于所使用的BCL的实现。编号的步骤非常简单。我添加了一些未经测试的代码来帮助您开始。您仍然需要自己进行冒泡排序,但是代码被标记在应该去的地方。它也未经测试。这意味着给你一个解决问题的开始,而不是为你做你的工作!谢谢@eriknoren我的冒泡排序不适用于(intj=0;j