I am trying to sort the array elements in a reverse order, but it doesn't seem to give me correct output. i have tried many times to correct it but not able to do so. I am a complete beginner. any help?
CODE
import java.util.Arrays;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
int[] arr = new int[5];
Scanner input = new Scanner(System.in);
System.out.println("Enter the array Elements");
for (int i = 0; i < 5; i++) {
arr[i] = input.nextInt();
}
swap(arr);
input.close();
}
static void swap(int arr[]) {
for (int start = 0; start < 5; start++) {
for (int end = arr.length - 1; end >= 0; end--) {
if (start >= end) {
System.out.println(Arrays.toString(arr));
System.exit(0);
} else {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
}
}
}
}
System.exit(), especially in a method that is just meant to reverse an array. If you want the method to stop, usereturn(or throw an exception, if you want to stop because of a problem).arrregularly, so you can see how it's being changed.