Write a complete main method that does the following: 1. Takes any number, but at least two, command line arguments which are numbers (represented as strings) and will print to the console the number of numbers that are evenly divisible by 10. (Hint: loop through the args array) 2. If there are not at least two command line arguments, throw an IllegalArgumentException with an appropriate message.

Respuesta :

Answer:

public static void main (String [] args) {

       

   public static void main (String [] args) {

       

       int count = 0;

       int[] arr = new int[args.length];

     if(args.length >=2) {

       for(int i=0; i<args.length; i++) {

           arr[i] = Integer.parseInt(args[i]);

           if(arr[i] % 10 == 0)

                      count++;

        }

     }

   else

        throw new IllegalArgumentException("Missing arguments!");

   

       System.out.println("There are " + count + " numbers that are divisible by 10");

   }

Explanation:

- Create a variable count to hold the count of the numbers divisible by 10

- Initialize an integer array to hold the inputs

- Check the number of arguments that are passed as a parameter. If it is greater than 2, parse the numbers into the arr. Then check the numbers that are divisible by 10, if number satisfies the condition, increment the count by 1. If the length of arguments passed is less than 2, throw an exception

Then:

Print the count