C# 在主C语言中获取一个方法# static void bubble(int[]mas) { 内部温度; 对于(int i=0;i

C# 在主C语言中获取一个方法# static void bubble(int[]mas) { 内部温度; 对于(int i=0;i,c#,C#,我需要使用方法来解决Uni项目的一些任务,我遇到的问题是我想在Main中创建一个数组,在不同的方法中使用它,并将这些方法的结果调用回Main。当我这样做,并试图调用“气泡”回到主,它告诉我,没有与形式给定参数对应的给定参数。有没有一种简单的方法可以解决这个问题,这样我就可以解决这个问题,并继续以类似的方式创建其他方法。 提前感谢您需要将调用中的参数传递到主气泡: static void bubble(int [] mas) { int temp;

我需要使用方法来解决Uni项目的一些任务,我遇到的问题是我想在Main中创建一个数组,在不同的方法中使用它,并将这些方法的结果调用回Main。当我这样做,并试图调用“气泡”回到主,它告诉我,没有与形式给定参数对应的给定参数。有没有一种简单的方法可以解决这个问题,这样我就可以解决这个问题,并继续以类似的方式创建其他方法。
提前感谢

您需要将调用中的参数传递到主气泡:

     static void bubble(int [] mas)
    {
        int temp;
        for (int i = 0; i < mas.Length; i++)
        {
            for (int j = 0; j < mas.Length - 1; j++)
                if (mas[j] > mas[j + 1])
                {
                    temp = mas[j + 1];
                    mas[j + 1] = mas[j];
                    mas[j] = temp;
                }

        }
        foreach (var element in mas)
        {
            Console.WriteLine("Sorted elements are: {0}", element);

        }

    }

    static void Main(string[] args)
    {
        int[] mas = new int[5];
        Console.WriteLine("Please enter the elements of the array: ");
        for (int i = 0; i < 5; i++)
        {
            mas[i] = Convert.ToInt32(Console.ReadLine());
        }
        bubble();
    }

出现错误的原因是函数
bubble
需要
int[]
作为参数

当前有
bubble()
,其当前状态为“无参数”


将其替换为
气泡(mas)

如下更改代码:

bubble(mas);
static int[]bubble(int[]mas)
{
内部温度;
对于(int i=0;imas[j+1])
{
温度=毫安时[j+1];
mas[j+1]=mas[j];
mas[j]=温度;
}
}
foreach(mas中的var元素)
{
WriteLine(“排序的元素是:{0}”,元素);
}
}
静态void Main(字符串[]参数)
{
int[]mas=新的int[5];
WriteLine(“请输入数组的元素:”);
对于(int i=0;i<5;i++)
{
mas[i]=Convert.ToInt32(Console.ReadLine());
}
气泡(mas);
}

在这种情况下,实现冒泡是您被赋予的任务的一部分,但一般来说,避免使用.Net已经实现的方法来实现排序算法。在这种情况下,类似于
List mylist=newlist(mas);mylist.Sort()
static int[] bubble(int [] mas)
    {
        int temp;
        for (int i = 0; i < mas.Length; i++)
        {
            for (int j = 0; j < mas.Length - 1; j++)
                if (mas[j] > mas[j + 1])
                {
                    temp = mas[j + 1];
                    mas[j + 1] = mas[j];
                    mas[j] = temp;
                }

        }
        foreach (var element in mas)
        {
            Console.WriteLine("Sorted elements are: {0}", element);

        }

    }

    static void Main(string[] args)
    {
        int[] mas = new int[5];
        Console.WriteLine("Please enter the elements of the array: ");
        for (int i = 0; i < 5; i++)
        {
            mas[i] = Convert.ToInt32(Console.ReadLine());
        }
        bubble(mas);
    }