There can be multiple solutions to this problem.
I'd work with the indices instead of the values/numbers, i.e. BiggestNumber and SmallestNumber being the indices of biggest and smallest number respectively. Then, I'd compare the value of IntArray[i] with IntArray[BiggestNumber] and IntArray[SmallestNumber] and if true, then store the index i
in BiggestNumber or SmallestNumber. Then in the end, after my for
loop, I'd swap the values in the array at both indices using another variable.
To point you into the direction:
int BiggestNumber = 0;
int SmallestNumber = 0;
if(IntArray[i] > IntArray[BiggestNumber])
{
BiggestNumber = i;
}
And to swap:
int a = IntArray[BiggestNumber];
IntArray[BiggestNumber] = IntArray[SmallestNumber];
IntArray[SmallestNumber] = a;
Hopefully you'll be able to fill in the missing parts I leave for you to fill in the assignment.
P.S.: I assume the method is for an instance/object level, i.e. non-static per your code, otherwise, I'd add keyword static
to it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…