Greater than, Less than or Equal to zero (Ver 2)

import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) { Integer[] numbers = new Integer[10]; ArrayList<Integer> greatZero = new ArrayList<>(); ArrayList<Integer> lessZero = new ArrayList<>(); ArrayList<Integer> zero = new ArrayList<>(); Scanner input = new Scanner(System.in); System.out.println("Enter 10 numbers: "); for (int x = 0; x < numbers.length; x++) { System.out.print(x+1 + "-> "); numbers[x] = input.nextInt(); int num = numbers[x]; if(num > 0) { greatZero.add(num); } else if(num < 0) { lessZero.add(num); } else { zero.add(num); } } Collections.sort(greatZero); Collections.sort(lessZero); Collections.reverse(lessZero); System.out.println(greatZero.size() + " Numbers > 0 ==> " + greatZero); System.out.println(lessZero.size() + " Numbers < 0 ==> " + lessZero); System.out.println(zero.size() + " Numbers = 0 ==> " + zero); } }
A program that asks the user to enter 10 integers of an array. The program computes how many integers are greater than, less than or equal to 0(zero) then sorts entered numbers in ascending order and finally print the size of each array with numbers in it.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.