[英]Sorting the grade and name of students inputting in C#
This is my code:这是我的代码:
I also want to get a number from the input of the number for a number of names, and at the end of the sort by the number and the name of the number entered in the output.我还想从多个名称的数字输入中获取一个数字,并在排序结束时按输出中输入的数字的数字和名称进行排序。
I am not permitted to use classes.我不被允许使用类。
int[] arr1 = new int[10];
string[] name = new string[10];
int n, i, j, tmp;
Console.Write("\n\nSort elements of array in ascending order :\n");
Console.Write("----------------------------------------------\n");
Console.Write("Input the size of array : ");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("Input {0} elements in the array :\n", n);
for (i = 0; i < n; i++)
{
Console.WriteLine("Enter name {0}", i);
name[i] = Console.ReadLine();
Console.Write("element - {0} : ", i);
arr1[i] = Convert.ToInt32(Console.ReadLine());
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (arr1[j] < arr1[i])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
Console.Write("\nElements of array in sorted ascending order:\n");
for (i = 0; i < n; i++)
{
Console.Write("{0}, {1} ", arr1[i],name[i]);
}
Console.Write("\n\n");
Console.ReadLine();
if you can use list type, you can try this code:如果你可以使用列表类型,你可以试试这个代码:
List<dynamic> lst= new List<dynamic>();
int n;
Console.Write ( "\n\nSort elements of array in ascending order :\n" );
Console.Write ( "----------------------------------------------\n" );
Console.Write ( "Input the size of array : " );
n = Convert.ToInt32 ( Console.ReadLine ( ) );
Console.Write ( "Input {0} elements in the array :\n", n );
for (var i = 0; i < n; i++ )
{
Console.WriteLine ( "Enter name {0}", i );
var name = Console.ReadLine ( );
Console.Write ( "element - {0} : ", i );
var elem = Convert.ToInt32 ( Console.ReadLine ( ) );
lst.Add ( new { name = name, elem = elem } );
}
Console.Write ( "\nElements of array in sorted ascending order:\n" );
lst.OrderBy ( t => t.elem ).ToList ( ).ForEach ( y => {
Console.Write ( "{0}, {1} ", y.elem, y.name );
} );
Console.Write ( "\n\n" );
Console.ReadLine ( );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.