Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class EvenNumberArrays {
- public static void main(String [] args)
- {
- //3)
- System.out.println("How many elements do you want to create for an array list? ");
- java.util.Scanner input = new java.util.Scanner(System.in);
- int length = input.nextInt();
- int[] list = new int[length];
- //4)
- System.out.println("Enter element values and the numbers of value will the same as you entered"
- + " above with the first question. ");
- //5) this is the way to enter each element for the list array.
- for(int i = 0; i < length; i++)
- {
- list[i] = input.nextInt();
- }
- //5) this explains when to call getNumOfEvens() method.
- System.out.println("It is time to call getNumOfEvens() method. ");
- System.out.println("Once you call that method, it will return number of evens");
- //6) actual calling getNumOfEvens() method by passing the array reference variable named list.
- System.out.println(getNumOfEvens(list));
- //7) this is how to call void method with this case.
- getNumOfEvensVoid(list);
- }
- //1) make sure change the original method of getNumberOfEvens to static as below.
- //2) just add static between public and int.
- public static int getNumOfEvens( int[] nums) {
- int count = 0;
- for(int i = 0; i < nums.length; i++)
- {
- if(nums[i] % 2 == 0)
- count++;
- }
- return count;
- }
- public static void getNumOfEvensVoid( int[] nums) {
- int count = 0;
- for(int i = 0; i < nums.length; i++)
- {
- if(nums[i] % 2 == 0)
- count++;
- }
- System.out.println("This arrays have " + count + " even numbers. ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement