omer omer - 3 years ago 134
Java Question

how to print this array in reverse

i just started learning java and i'm trying to print this array in reverse
not sure what i'm doing wrong but it doesn't return the second loop, no errors

package com.company;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {

Scanner red = new Scanner(System.in);
int[] arr;
arr = new int[3];
for (int i=0;i<arr.length;i++)
{
System.out.println("enter number");
arr[i]=red.nextInt();

}
for (int j=2;j<0;j--) {
System.out.println(arr[j]);
}
}
}

Answer Source

Try this

for (int j = arr.length-1; j >= 0; j--) {
            System.out.println(arr[j]);
        }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download