C# 交换firstInt、middleInt和lastin时出现问题

C# 交换firstInt、middleInt和lastin时出现问题,c#,C#,我试图让Main方法声明三个名为firstInt、middleInt和lastin的整数。并将这些值分配给变量,显示它们,然后将它们传递给一个方法,该方法接受它们作为引用变量,将第一个值放置在lastin变量中,将最后一个值放置在firstInt变量中。在主方法中,再次显示三个变量,证明其位置已反转 static void Main(string[] args) { int first = 33; int middle = 44; int last = 55;

我试图让Main方法声明三个名为firstInt、middleInt和lastin的整数。并将这些值分配给变量,显示它们,然后将它们传递给一个方法,该方法接受它们作为引用变量,将第一个值放置在lastin变量中,将最后一个值放置在firstInt变量中。在主方法中,再次显示三个变量,证明其位置已反转

static void Main(string[] args)
{

    int first = 33;
    int middle = 44;
    int last = 55;

    Console.WriteLine("Before the swap the first number is {0}", first);
    Console.WriteLine("Before the swap the middle number is {0}", middle);
    Console.WriteLine("Before the swap the last number is {0}", last);

    Swap(ref first, ref middle, ref last);
    Console.WriteLine("\n============AFTER===THE===SWAP======================");
    Console.WriteLine("After the swap the first is {0}", first);
    Console.WriteLine("After the swap the middle is {0}", middle);
    Console.WriteLine("After the swap the last is {0}", last);
}

private static void Swap(ref int first, ref int middle, ref int last);

   int temp;
    temp = firstInt;
    firstInt = lastInt;
    lastInt = temp;

   }
}
您在first和firstInt、last和lastInt之间混淆:

这就是问题所在:

private static void Swap(ref int first, ref int middle, ref int last);

   int temp;
    temp = firstInt;
    firstInt = lastInt;
    lastInt = temp;

   }
你有一个;在方法交换的参数列表之后,当它应该是{大括号时:

private static void Swap(ref int first, ref int middle, ref int last)
{

    int temp;
    temp = firstInt;
    firstInt = lastInt;
    lastInt = temp;
}
您的代码生成类型或命名空间定义,或预期文件结尾。错误

编辑

正如其他人指出的,您也有错误的变量名称-它应该是first、middle和last,所以您的整个方法应该是:

private static void Swap(ref int first, ref int middle, ref int last)
{

    int temp;
    temp = first;
    first = last;
    last = temp;
}

不知道我为什么要回答,但这都是简单的编译问题

   private static void Swap(ref int first, ref int middle, ref int last); <-- semicolon should not be here
      <-- missing bracket
       int temp;
        temp = firstInt; <-- none of these names exist
        firstInt = lastInt;
        lastInt = temp;

    }

或者使用一些XOR交换,没有温度变量:

private static void Swap(ref int first, ref int middle, ref int last) {
    first ^= last;
    last ^= first;
    first ^= last;
}

输出有什么问题?输出应该是什么?它说在名称空间中找不到它?代码中有什么问题吗?至少给出一个可以编译的示例。如果你修复了编译器警告,那么这工作正常,你到底期望什么?@user2676862什么找不到..什么名称空间???opps没有看到,谢谢。噢我的错误是把first和last的名称与firstInt和lastin弄混了。我用int-temp=first声明了temp;是的,但是我的版本在没有temp变量的情况下做了同样的事情。
    private void Swap(ref int first, ref int middle, ref int last)
    {
        int temp;
        temp = first;
        first = last;
        last = temp;
    }
private static void Swap(ref int first, ref int middle, ref int last) {
    first ^= last;
    last ^= first;
    first ^= last;
}