Computer Language/유니티
swap 배열 사용시 기초 예제
알 수 없는 사용자
2014. 4. 22. 10:23
.4453125void Start () {
int a = 0, b = 0, c = 0;
Swap (ref a, ref b);
print (a + " " + b);
int[] score = new int[3]{40, 30, 20} ;
if(score[1] < score[0])
ArraySwap(ref score[0], ref score[1]);
if(score[2] < score[1])
ArraySwap(ref score[1], ref score[2]);
if(score[2] < score[0])
ArraySwap(ref score[0], ref score[2]);
if(score[1] < score[0])
ArraySwap(ref score[0], ref score[1]);
for(int i = 0; i < 3; i++)
print (score[0] + " " + score[1] + " " + score[2]);
}
void Swap(ref int a, ref int b){
int temp = a;
a = b;
b = temp;
}
void ArraySwap(ref int a, ref int b){
int temp = a;
a = b;
b = temp;
}