【问题标题】:output of zeros when i try to replace array elements当我尝试替换数组元素时输出零
【发布时间】:2020-01-20 16:32:12
【问题描述】:
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]);
        }
    }
}

【问题讨论】:

  • 您好,欢迎来到 StackOverflow。你好像忘了问这个问题。你期望代码做什么,错误是什么,如果你得到一个,预期的输出是什么?您可能还想添加“java”作为标签。

标签: arrays replace output


【解决方案1】:
    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]);
    }

【讨论】:

    猜你喜欢
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 2018-08-07
    • 2018-09-05
    相关资源
    最近更新 更多