C#怎么使用自定义方法给数组冒泡排序 如题:输入5个人的成绩 然后自定义方法排序。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 为什么不选择系统提供的。Array.Sort(你的数组); using System;using System.Collections.Generic;using System.Text;namespace Text{ class Program { static void Main(string[] args) { int[] count = new int[5]; // 需要排序的数组 int i, j; // 循环变量 int temp; // 临时变量 // 读入数组 Console.WriteLine("请输入5个数:"); for (i = 0; i < 5; i++) { Console.WriteLine("输入第{0}个数:",i+1); count[i] = int.Parse(Console.ReadLine()); // 类型转换 } // 开始排序 -----------冒泡排序 for (i = 0; i < count.Length-1; i++) // 控制比较多少轮 { // 将最大的元素交换到最后 for (j = 0; j < count.Length-1-i; j++) { if(count[j] > count[j+1]) { // 交换元素 temp = count[j]; count[j] = count[j + 1]; count[j + 1] = temp; } } } // 排序后输出 Console.WriteLine("排序后:"); for (i = 0; i < count.Length; i++) { Console.WriteLine("{0}\t",count[i]); } Console.ReadLine(); } }} static void Main(string[] args) { int[] count = new int[5]; // 需要排序的数组 Console.WriteLine("请输入5个数:"); for (int i = 0; i < 5; i++) { Console.WriteLine("输入第{0}个数:", i + 1); count[i] = int.Parse(Console.ReadLine()); // 类型转换 } // 开始排序 -----------冒泡排序方法 NewMethod1(ref count); // 排序后输出 for (int i = 0; i < count.Length; i++) { Console.WriteLine("{0}\t", count[i]); } } private static void NewMethod1(ref int[] count) { int temp; for (int i = 0; i < count.Length - 1; i++) // 控制比较多少轮 { // 将最大的元素交换到最后 for (int j = 0; j < count.Length - 1 - i; j++) { if (count[j] > count[j + 1]) { // 交换元素 temp = count[j]; count[j] = count[j + 1]; count[j + 1] = temp; } } } } class MyArray{ public string mySort(string[] oldStr){ string[] resultStr=String.Empty; // 开始排序 ----laoas兄的代码-------冒泡排序 return resultStr; }}MyArray arrSort = ne MyArray();arrSort.mySort(需要排序的字符串数组); 谢谢了 ,原来我自己调用方法的时候ref 后面跟的数组, 初学 还不太会用, 求助在webbrowser自动点击某一超链接 [求助]关于如何判断2005中的水晶报表的中英文? c#中2个DataSet合并的问题 c#开发的ASP.NET中调用dll时,结果出人意料 C# 如何去除掉TXT文件的头尾的空格(不改变其它的格式),然后保存? C#播放声音的问题 C#中如何历遍XML并将值按节点次序添加到TREEVIEW 朋友帮我写个判断语句哦.很简单的.我不会.呵呵 请问微软哪个认证适合做软件的人考? OpenFileDialog 获取文件路径 窗体如何在两个工程间复制 CheckedListBox打勾、取消打勾触发哪个事件?
Array.Sort(你的数组);
using System.Collections.Generic;
using System.Text;namespace Text
{
class Program
{
static void Main(string[] args)
{
int[] count = new int[5]; // 需要排序的数组
int i, j; // 循环变量
int temp; // 临时变量 // 读入数组
Console.WriteLine("请输入5个数:");
for (i = 0; i < 5; i++)
{
Console.WriteLine("输入第{0}个数:",i+1);
count[i] = int.Parse(Console.ReadLine()); // 类型转换 }
// 开始排序 -----------冒泡排序
for (i = 0; i < count.Length-1; i++) // 控制比较多少轮
{
// 将最大的元素交换到最后
for (j = 0; j < count.Length-1-i; j++)
{
if(count[j] > count[j+1])
{
// 交换元素
temp = count[j];
count[j] = count[j + 1];
count[j + 1] = temp;
}
}
}
// 排序后输出
Console.WriteLine("排序后:");
for (i = 0; i < count.Length; i++)
{
Console.WriteLine("{0}\t",count[i]);
}
Console.ReadLine();
}
}
}
{
int[] count = new int[5]; // 需要排序的数组
Console.WriteLine("请输入5个数:");
for (int i = 0; i < 5; i++)
{
Console.WriteLine("输入第{0}个数:", i + 1);
count[i] = int.Parse(Console.ReadLine()); // 类型转换 }
// 开始排序 -----------冒泡排序方法
NewMethod1(ref count);
// 排序后输出
for (int i = 0; i < count.Length; i++)
{
Console.WriteLine("{0}\t", count[i]);
}
}
private static void NewMethod1(ref int[] count)
{
int temp;
for (int i = 0; i < count.Length - 1; i++) // 控制比较多少轮
{
// 将最大的元素交换到最后
for (int j = 0; j < count.Length - 1 - i; j++)
{
if (count[j] > count[j + 1])
{
// 交换元素
temp = count[j];
count[j] = count[j + 1];
count[j + 1] = temp;
}
}
}
}
class MyArray{
public string mySort(string[] oldStr){
string[] resultStr=String.Empty;
// 开始排序 ----laoas兄的代码-------冒泡排序
return resultStr;
}
}MyArray arrSort = ne MyArray();
arrSort.mySort(需要排序的字符串数组);
初学 还不太会用,