I want Java to print the elements of the array in reverse order.

Asked 2 years ago, Updated 2 years ago, 25 views

I am a beginner in Java.I'm working on Java sequencing for my school assignment.
For the time being, I was able to enter the input value with the keyboard in the array.
It seems different from the image.

A program that puts user-entered values into an array and finally outputs the values in the array in reverse order.
The image looks like this:

Prompt and User Input:

Enter an integer
3
2
1

Output:

1
2
3

I would appreciate it if you could point out any mistakes in the source code.

public static void main(String[]args){

    // array s initialization
    int[]s = new int[10];
    // receive 10 inputs
    for(inti=0;i<s.length-1;i++){
        System.out.println("Enter an integer");
        Scanner scan1 = new Scanner (System.in);
        s[i] = scan1.nextInt();
        System.out.println(s[i]);
    }
}

java

2022-09-29 22:49

5 Answers

If you put the input of numbers and output of results together in one loop, of course the results will only be output in the order of input.I think we need to prepare two loops (enter numbers, output results in reverse order).

When outputting the results, I think you can output them in reverse order by using the for statement while -1 index by array size.


2022-09-29 22:49

Please enter an integer and if you want to accept user input 10 times and display it in reverse order, the code should be as follows:

public static void main(String[]args){
    // array s initialization
    int[]s = new int[10];
    System.out.println("Enter an integer");
    // receive 10 inputs
    try(Scanner scan1=new Scanner(System.in);){
        for(inti=0;i<s.length;i++){
            s[i] = scan1.nextInt();
        }
        for(inti=s.length;i>0;i--){
            System.out.println(s[i-1]);
        }
    } catch(Exceptione){
        e.printStackTrace();
    }
}

<scan1 should call close() at the end, so it is better to use try-with-resources(If it's just a main method like this, you don't have to worry about it, but in some cases it leaks resources.)If Java 6 or earlier is missing try-with-resources, run scan1.close(); in the finally clause.


2022-09-29 22:49

If you don't have a Java version, how about this one?

import java.util.Scanner;
import java.util.stream.IntStream;

public class SchoolChallenges {

public static void main(String[]args) {
    elementCount = 10;
    int[]s = new int [elementCount];
    System.out.println("Enter an integer");
    try(Scanner scan1=new Scanner(System.in);){
        IntStream.range(0,elementCount).forEach(e->{
            s[e] = scan1.nextInt();
        });

        System.out.println("Output in reverse order of input";
        IntStream.rangeClosed(1,elementCount)
                .boxed()
                .mapToInt(i->s[elementCount-i])
                .forEach (System.out:println);

    } catch(Exceptione){
        e.printStackTrace();
    }

}

}


2022-09-29 22:49

For input followed by

for(inti=s[i].length;i>0;i--){
    System.out.println(s[i-1]);
}

If you print it out in , what do you think?


2022-09-29 22:49

Is it like this?
I didn't test it.

public static void main(String[]args){

    // array s initialization
    int[]s = new int[10];
    // receive 10 inputs
    for(inti=0;i<s.length-1;i++){
        System.out.println("Enter an integer");
        Scanner scan1 = new Scanner (System.in);
        s[i] = scan1.nextInt();
    }
    for(inti=0;i<s.length-1;i++){
        System.out.println(s[(s.length-1)-i]);
    }
}


2022-09-29 22:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.