0

The code to return the integer array reversed. I think its entirely correct, But its not working.

The code is not entering into the for(int j=3; j==0; j--) loop.

CODE:

import java.io.*;
import java.util.*;


public class Solution {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int[] arr = new int[n];
    int[] rev = new int[n];

  for(int i=0; i < n; i++)
    {
        arr[i] = in.nextInt();   
      System.out.println( "Here"+ arr[i]);
      for(int j=3; j==0; j--)
       {
          System.out.println(j);
          System.out.print( "Resevre1 "+ rev[j]);
          if (j+i==3)
          {
          rev[j] = arr[3-i];
          System.out.print( "Resevre Here"+ rev[j]);
          }
       }
    }
    in.close();
}
}

Sample Input

4

1 4 3 2

Sample Output

2 3 4 1

1
  • 1
    for(int j=3; j==0; j--) means you tantalize j to 3 and execute the loop as long as j equals 0. Commented Jun 18, 2017 at 8:52

2 Answers 2

1

The code wont go in the loop the condituon you have entered will always be false.

for(int j=3; j==0; j--)

inital value of j is 3 and the condition statement which needs to be true everytime you want your program to go inside the loop this will always keep failling unless your condition statement j==0

satisfies. As I can judge you are trying to run the loop in reverse You need to change the condition to for(int j= 3; j>=0; j--)

depands how many times you want this loop to run revrse thrice or twice.

Sign up to request clarification or add additional context in comments.

Comments

0

CODE:

import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];

        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }

        for (int i = n - 1; i >= 0; i--) {
            System.out.print(arr[i] + " ");
        }

        in.close();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.