在数组中输入数字并显示它?

时间:2015-09-26 17:36:18

标签: java arrays loops

如何制作基于用户输入的数组?然后按降序显示它? 例如

输入:

Input Number of array = 3

Input no 1 = 2

Input no 2 = 4

Input no 3 = 7

Output: 7 , 4 , 2

1 个答案:

答案 0 :(得分:0)

我认为你是初学者。所以我会帮助你站起来。试试这段代码。

class InputTest {

    public static void main(String[] args) {
        System.out.print("Enter size of array: ");
        Scanner scanner = new Scanner(System.in);

        int numberOfArray = scanner.nextInt();
        Integer[] input = new Integer[numberOfArray];

        for (int i = 0; i < numberOfArray; i++) {
            System.out.print("Input number " + (i + 1) + ": ");
            input[i] = scanner.nextInt();
        }

        Arrays.sort(input, Collections.reverseOrder());

        for (int i : input) {
            System.out.print(i + ",");
        }
    }
}