Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何更改表中数字的顺序_C# - Fatal编程技术网

C# 如何更改表中数字的顺序

C# 如何更改表中数字的顺序,c#,C#,我刚学会如何制作10、20或25数字表。我正在做10号桌。例如,我有4个数字表1,2,3,4,想在最后写成4,3,2,1-有人能告诉我怎么做吗 我的节目 static void Main(string[] args) { int[] p = new int[10] { 2, 9, 13, 3, 50, -8, -30, 0, 1, 4 }; int vsota = 0; float povprecje = 0; f

我刚学会如何制作10、20或25数字表。我正在做10号桌。例如,我有4个数字表1,2,3,4,想在最后写成4,3,2,1-有人能告诉我怎么做吗

我的节目

    static void Main(string[] args)
    {
        int[] p = new int[10] { 2, 9, 13, 3, 50, -8, -30, 0, 1, 4 };
        int vsota = 0;
        float povprecje = 0;

        for (int i = 0; i < p.Length; i++)
        {
            Console.WriteLine("p [{0}] = {1,3}", i, p[i]);
            vsota += p[i];
        }
        povprecje = (float)vsota/p.Length;
        Console.WriteLine();
        Console.WriteLine("Vsota = {0}!", vsota);
        Console.WriteLine("Povprecje = {0}!", povprecje);
        Console.ReadKey(true);
    }
static void Main(字符串[]args)
{
int[]p=新的int[10]{2,9,13,3,50,-8,-30,0,1,4};
int-vsota=0;
浮点数povprecje=0;
对于(int i=0;i
如果您使用的是列表,您可以使用:

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        List<int> table = new List<int>();

        table.Add(1);
        table.Add(2);
        table.Add(3);
        table.Add(4);

        foreach(int item in table)
        {
            Console.WriteLine(item);
        }

        table.Reverse();

        Console.WriteLine();
        foreach(int item in table)
        {
            Console.WriteLine(item);
        }
    }
}
使用系统;
使用System.Collections.Generic;
公开课范例
{
公共静态void Main()
{
列表=新列表();
表.增加(1);
表.增加(2);
表.增加(3);
表.增加(4);
foreach(表中的int项)
{
控制台写入线(项目);
}
表1.Reverse();
Console.WriteLine();
foreach(表中的int项)
{
控制台写入线(项目);
}
}
}

如果您使用的是列表,您可以使用:

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        List<int> table = new List<int>();

        table.Add(1);
        table.Add(2);
        table.Add(3);
        table.Add(4);

        foreach(int item in table)
        {
            Console.WriteLine(item);
        }

        table.Reverse();

        Console.WriteLine();
        foreach(int item in table)
        {
            Console.WriteLine(item);
        }
    }
}
使用系统;
使用System.Collections.Generic;
公开课范例
{
公共静态void Main()
{
列表=新列表();
表.增加(1);
表.增加(2);
表.增加(3);
表.增加(4);
foreach(表中的int项)
{
控制台写入线(项目);
}
表1.Reverse();
Console.WriteLine();
foreach(表中的int项)
{
控制台写入线(项目);
}
}
}
您可以使用和方法对集合进行排序。在您的示例中:

int[] p = new int[10] { 2, 9, 13, 3, 50, -8, -30, 0, 1, 4 };
p = p.OrderByDescending( x => x ).ToArray();
int[] b = a.Reverse.ToArray();
编辑:将表达式添加到OrderByDescending方法中。此外,Linq用于3.5及更高版本的框架,需要引用System.Linq命名空间

您可以使用和方法对集合进行排序。在您的示例中:

int[] p = new int[10] { 2, 9, 13, 3, 50, -8, -30, 0, 1, 4 };
p = p.OrderByDescending( x => x ).ToArray();
int[] b = a.Reverse.ToArray();

编辑:将表达式添加到OrderByDescending方法中。此外,Linq用于3.5及更高版本的框架,需要引用System.Linq命名空间

我想你的意思是你想反转一个数组。因此,如果您有此阵列:

int[] a = new int[] { 0, 1, 2, 3 };
您希望反转元素,使其
{3,2,1,0}

关键是要交换元素。例如,如果您要编写:

// swap first and last elements
int temp = a[0];
a[0] = a[a.Length - 1];
a[a.Length - 1] = temp;
那么您的数组将是
{3,1,2,0}

因此,我们的想法是交换
a[0]
a[length-1]
。然后交换
a[1]
a[length-2]
等,直到您将每个项目与其对应项交换

您可以使用单个循环来完成此操作

顺便说一下,
Reverse
方法仅适用于
列表
类型。对于数组,
Reverse
方法实际上是一种LINQ扩展方法,您需要创建一个新数组。例如:

int[] p = new int[10] { 2, 9, 13, 3, 50, -8, -30, 0, 1, 4 };
p = p.OrderByDescending( x => x ).ToArray();
int[] b = a.Reverse.ToArray();

我想你的意思是你想反转一个数组。因此,如果您有此阵列:

int[] a = new int[] { 0, 1, 2, 3 };
您希望反转元素,使其
{3,2,1,0}

关键是要交换元素。例如,如果您要编写:

// swap first and last elements
int temp = a[0];
a[0] = a[a.Length - 1];
a[a.Length - 1] = temp;
那么您的数组将是
{3,1,2,0}

因此,我们的想法是交换
a[0]
a[length-1]
。然后交换
a[1]
a[length-2]
等,直到您将每个项目与其对应项交换

您可以使用单个循环来完成此操作

顺便说一下,
Reverse
方法仅适用于
列表
类型。对于数组,
Reverse
方法实际上是一种LINQ扩展方法,您需要创建一个新数组。例如:

int[] p = new int[10] { 2, 9, 13, 3, 50, -8, -30, 0, 1, 4 };
p = p.OrderByDescending( x => x ).ToArray();
int[] b = a.Reverse.ToArray();

要对一维数组进行排序,可以调用array.sort()和/或array.Reverse()方法。更新示例时,可以执行以下操作:

int[] p = new int[10] { 2, 9, 13, 3, 50, -8, -30, 0, 1, 4 };
int vsota = 0;
float povprecje = 0;

for (int i = 0; i < p.Length; i++)
{
    Console.WriteLine("p [{0}] = {1,3}", i, p[i]);
    vsota += p[i];
}
povprecje = (float)vsota/p.Length;
Console.WriteLine();
Console.WriteLine("Vsota = {0}!", vsota);
Console.WriteLine("Povprecje = {0}!", povprecje);
Console.ReadKey(true);

// Sort (low to high) the int[] by calling the Array.Sort() method
Array.Sort(p);
foreach(int i in p)
{
    Console.WriteLine(i);
}

// Sort (high to low) the int[] by calling the Array.Reverse() method
Array.Reverse(p);
foreach(int i in p)
{
    Console.WriteLine(i);
}
int[]p=newint[10]{2,9,13,3,50,-8,-30,0,1,4};
int-vsota=0;
浮点数povprecje=0;
对于(int i=0;i
要对一维数组进行排序,可以调用array.sort()和/或array.Reverse()方法。更新示例时,可以执行以下操作:

int[] p = new int[10] { 2, 9, 13, 3, 50, -8, -30, 0, 1, 4 };
int vsota = 0;
float povprecje = 0;

for (int i = 0; i < p.Length; i++)
{
    Console.WriteLine("p [{0}] = {1,3}", i, p[i]);
    vsota += p[i];
}
povprecje = (float)vsota/p.Length;
Console.WriteLine();
Console.WriteLine("Vsota = {0}!", vsota);
Console.WriteLine("Povprecje = {0}!", povprecje);
Console.ReadKey(true);

// Sort (low to high) the int[] by calling the Array.Sort() method
Array.Sort(p);
foreach(int i in p)
{
    Console.WriteLine(i);
}

// Sort (high to low) the int[] by calling the Array.Reverse() method
Array.Reverse(p);
foreach(int i in p)
{
    Console.WriteLine(i);
}
int[]p=newint[10]{2,9,13,3,50,-8,-30,0,1,4};
int-vsota=0;
浮点数povprecje=0;
对于(int i=0;i
要反转阵列:

p = p.Reverse().ToArray();
要反转阵列,请执行以下操作:

p = p.Reverse().ToArray();

这是@Metro Smurf的LINQ解决方案的更新:

int[] p = new int[10] { 2, 9, 13, 3, 50, -8, -30, 0, 1, 4 };
p = p.OrderByDescending(x => x).ToArray();

这是@Metro Smurf的LINQ解决方案的更新:

int[] p = new int[10] { 2, 9, 13, 3, 50, -8, -30, 0, 1, 4 };
p = p.OrderByDescending(x => x).ToArray();

什么是桌子?我不明白你的问题。你能提供一些你想做的代码吗?静态void Main(string[]args){int[]p=new in