当我尝试替换数组元素时为零输出

问题描述 投票:0回答:1
import java.util.*;

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

        Scanner num = new Scanner(System.in);
        System.out.println("Enter the array size : ");
        int size = num.nextInt();

        int array[] = new int[size];
        for (int i = 0; i < size; i++) {
            System.out.println("Enter the " + (i + 1) + " number of array");
            int m = num.nextInt();
        }

        int repl = 0 ; 

        for(int i = 0 ; i < array.length ;i++) {
            repl = array[i];
            array[i] = array[array.length-1-i];
        }
        for(int i =0 ; i< array.length ; i++) {
            System.out.println(array[i]);
        }
    }
}
arrays replace output
1个回答
0
投票
    Scanner num = new Scanner(System.in);
    System.out.println("Enter the array size : ");
    int size = num.nextInt();

    int array[] = new int[size];
    for (int i = 0; i < size; i++) {
        System.out.println("Enter the " + (i + 1) + " number of array");
        int m = num.nextInt();
        array[i] = m;
    }

    int repl = 0;

    for (int i = 0; i < (array.length/2); i++) {
        repl = array[i];
        array[i] = array[array.length - 1 - i];
        array[array.length - 1 - i] = repl;

    }
    for (int i = 0; i < array.length; i++) {
        System.out.println(array[i]);
    }
© www.soinside.com 2019 - 2024. All rights reserved.