关于C#的一个简单问题

来源:百度知道 编辑:UC知道 时间:2024/05/28 19:24:39
//此方法负责数组的输入输出
static void Main(string[] args)
{
//定义一个整数数组
int[] sort=new int[8];
int[] sorts = new int[8];

Console.WriteLine("请连续输入8个整数:");
//使用for循环向数组中添加数据
for (int i = 0; i < sort.Length;i++ )
{
sort[i] =Convert.ToInt32(Console.ReadLine());
}

Taxis(sort);
}

//此方法用来使用冒泡排序
static int[] Taxis( int[] sort)
{
//得到数组的值后,进行排序
for (int i = 0; i < sort.Length - 1;i++ )
{
for (int j = 0; j < sort.Length - 1 - i;j++ )
{
if(sort[j]<sort[j+1])
{
int temp;
temp = sort[j];
sort[j

实际上,对数组的修改并非值修改,而是引用修改,因此不需要重新赋值另外一个数组,sort数组中已经正确保存排序后的结果。可以直接在Taxis(sort)之后输入:
for (int i = 0; i < sort.Length; i++)
{
Console.WriteLine(sort[i]);
}
自然得到排序后的结果了。

另外说一下,你的程序中最好说明一下是每次输入数字后回车确认,否则用户在连续输入数字后回车将会遇到因为类型检查而报的错

可以有For,你再试一下吧朋友

用一个数组比如
int[] a=Taxis(sort) ;接收之后就直接输出a的元素了
试试?