当前位置:编程学习 > C#/ASP.NET >>

c#动态数组

c#动态数组,从控制台输入数组并排序后输出
答案:
在vs2005中创建控制台管理程序后写入:
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int n;
Console.WriteLine("请你输入数组的长度:\n");
n=Convert.ToInt32(Console.ReadLine());
int[] arr = new int[n];
for (int i = 0; i < n; i++)
{
Console.WriteLine("请你输入第{0}个元素:",i);
arr[i] = Convert.ToInt32(Console.ReadLine());
}

for (int i = 1; i < n; i++)
{
for (int j = 0; j < n - i; j++)
{
if (arr[j] > arr[j + 1])
{
int temp;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
Console.WriteLine("\n经过排序之后的结果为:\n");
foreach (int k in arr)
{
Console.Write(k + " ");

}
Console.WriteLine("\n");
Console.ReadLine ();
}


}

}

将文件保存为progran.cs,在运行,就可以了
.sort方法
定义数组
数组有一个sort方法
可以按你需要的排序

上一个:c#编程问题
下一个:C#截图源代码

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,