TransWikia.com

Can someone teach me how to get this output

Stack Overflow Asked by surtz on December 20, 2020

I’m new to java. I’m trying to make my program output this [5,4] [3] [2,1] but instead I get [5,5] [4,4] [3,3] [2,2] [1,1].. what am I missing? I tried to answer it on my own but I just can’t think of the answer.

Here’s my full code:

   public static void main(String[]args){
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter Array Size:");
    int arrSize = sc.nextInt();
    System.out.println("The Size of your Array is "+ arrSize);
    int arr[] = new int[arrSize];
    System.out.println("Enter "+arrSize+" Elements of your Array: ");
    for(int i=0;i<arr.length;i++){
        arr[i] = sc.nextInt();
    }
    for(int i=0; i<arr.length;i++){
        System.out.print(arr[i] + " ");
    }
    System.out.println(" ");
    for(int i=arr.length-1; i>=0;i--){
        System.out.print(Arrays.asList(arr[i]+","+arr[i]));
    }

}

2 Answers

try this code

        Scanner sc=new Scanner(System.in);
        System.out.print("Enter Array Size:");
        int f,midd=0;
        int arrSize = sc.nextInt();
        System.out.println("The Size of your Array is "+ arrSize);
        int arr[] = new int[arrSize];
        System.out.println("Enter "+arrSize+" Elements of your Array: ");
        for(int i=0;i<arr.length;i++){
            arr[i] = sc.nextInt();
        }
        if(arrSize%2==0){
            f=0;
        }
        else {
            f=1;
            midd=(int)(arrSize/2/2)+1;
        }
        for(int i=arrSize-1; i>=0;){
            if(f==0)
            {
                System.out.print(Arrays.asList(arr[i]+","+arr[i-1]));
                i-=2;
            }
            else{
                if(midd==i){
                    System.out.print(Arrays.asList(arr[i]));
                    i--;
                }
                else {
                    System.out.print(Arrays.asList(arr[i]+","+arr[i-1]));
                    i-=2;
                }
            }
        }

provide this program inside main method Output:

Enter Array Size:5
The Size of your Array is 5
Enter 5 Elements of your Array: 
1
2
3
4
5
[5,4][3][2,1]

.

Enter Array Size:4
The Size of your Array is 4
Enter 4 Elements of your Array: 
1
2
3
4
[4,3][2,1]

.

Enter Array Size:7
The Size of your Array is 7
Enter 7 Elements of your Array: 
1
2
3
4
5
6
7
[7,6][5,4][3][2,1]

Correct answer by sourab maity on December 20, 2020

YOu can try the following. I think what you need is to split the array as a pair.

I am assuming you will have middle pair with one element in case of odd length and all pairs will have two elements in case of even length of the array.

public static void main(String[]args){
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter Array Size:");
    int arrSize = sc.nextInt();
    System.out.println("The Size of your Array is "+ arrSize);
    int arr[] = new int[arrSize];
    System.out.println("Enter "+arrSize+" Elements of your Array: ");
    for(int i=0;i<arr.length;i++){
        arr[i] = sc.nextInt();
    }
    for(int i=0; i<arr.length;i++){
        System.out.print(arr[i] + " ");
    }
    System.out.println(" ");
    int i=arr.length-1;
    for(; i>arr.length/2;i-=2){
        System.out.print(Arrays.asList(arr[i]+","+arr[i-1]));
    }
    if(arr.length %2 == 0){
        System.out.print(Arrays.asList(arr[i]+","+arr[i-1]));
        i-=2;
    }else{
        System.out.print(Arrays.asList(arr[i]));
        i-=1;
    }
    for(; i>0;i-=2){
        System.out.print(Arrays.asList(arr[i]+","+arr[i-1]));
    }

}

Answered by Lone wolf on December 20, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP