for (int j=1;j <=a.Length;j++) 这句数组超了...是排序么?呀 你那个排的好像不对吧..好像结果不对.试试我这个.
int[] a = new int[] { 2, 5, 6, 1, 30, 3 };
int b;
for(int i=0; i<a.Length-1; i++)
{
for(int j=i+1; j<a.Length; j++)
{
if(a[i]<a[j])
{
b=a[i];
a[i]=a[j];
a[j]=b;
}
}
}
for (int i = 0; i < a.Length; i++)
{
Console.BackgroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write("{0}", a[i]);
}
Console.Read();
int[] a = new int[] { 2, 5, 6, 1, 30, 3 };
int b;
for(int i=0; i<a.Length-1; i++)
{
for(int j=i+1; j<a.Length; j++)
{
if(a[i]<a[j])
{
b=a[i];
a[i]=a[j];
a[j]=b;
}
}
}
for (int i = 0; i < a.Length; i++)
{
Console.BackgroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write("{0}", a[i]);
}
Console.Read();
解决方案 »
- 用writefile给usb设备发送数据的时候,出现了用户请求的buffer无效
- Application.AddMessageFilter(this);在WPF里面应该用什么等效?
- 问个简单问题(tostring参数)
- 用.net 怎么读取邮件
- 寻求日历控件
- 熟悉DataGrid/DataList/Repeater三者之一的兄弟进来帮个忙
- c#画图时候关于清屏的问题
- 如何屏蔽掉输入textbox中除了的数字以外的字符?
- 在datagrid如何更改某个特定行的背景色?谢谢
- 怎么实现Socket每隔一段时间发送请求直到收到该请求对应的回应或者超时
- 从文中挑选出文字字符,数字和其他字符 并统计出其个数
- c#2005 用按钮实现循环的中止
for (int i = 0; i < a.Length - 1; i++)
{
for (int j=1;j <a.Length;j++)
{
b = a[j];
a[j] = a[i];
a[i] = b;
}
}
int k = 0;
bool flag = false;
for(int i=0; i<a.Length-1; i++)
{
k = i;
flag = true;
for(int j=i+1; j<a.Length; j++)
{
if(a[k]<a[j])
{
k = j;
flag = false;
}
}
if (flag) break;
if (i != k)
{
a[i] = a[i] + a[k];
a[k] = a[i] - a[k];
a[i] = a[i] - a[k];
}
}
for (int i = 0; i < a.Length; i++)
{
Console.BackgroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write("{0}", a[i]);
}
Console.Read();
for (int i = 0; i <= a.Length - 1; i++)
{
for (int j=1;j <=a.Length;j++)
{
b = a[j];
a[j] = a[i];
a[i] = b;
}
} 哪里有比较?
索引是从o开始到长度-1结束