public class Swap {
void swap(int x, int y){
int tmp = x;
x = y;
y = tmp;
}
public static void main(String[] args) {
Swap s = new Swap();
int x = 10;
int y = 20;
System.out.println("[変更前]");
System.out.println("x="+x+", y="+y);
s.swap(x, y);
System.out.println("[変更後]");
System.out.println("x="+x+", y="+y);
}
}
|
namespace Swap_CSharp
{
class Swap
{
void swap(int x, int y)
{
int tmp = x;
x = y;
y = tmp;
}
static void Main(string[] args)
{
Swap s;
s = new Swap();
int x = 10;
int y = 20;
Console.WriteLine("[変更前]");
Console.WriteLine("x=" + x + ", y=" + y);
s.swap(x, y);
Console.WriteLine("[変更後]");
Console.WriteLine("x=" + x + ", y=" + y);
}
}
}
|
// swap 関数の定義部 void swap(ref int x, ref int y) // swap 関数の呼び出し部 s.swap(ref x, ref y); |