Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. public class EvenNumberArrays {
  2.  
  3. public static void main(String [] args)
  4. {
  5.  
  6. //3)
  7. System.out.println("How many elements do you want to create for an array list? ");
  8. java.util.Scanner input = new java.util.Scanner(System.in);
  9. int length = input.nextInt();
  10. int[] list = new int[length];
  11.  
  12. //4)
  13. System.out.println("Enter element values and the numbers of value will the same as you entered"
  14. + " above with the first question. ");
  15. //5) this is the way to enter each element for the list array.
  16. for(int i = 0; i < length; i++)
  17. {
  18. list[i] = input.nextInt();
  19. }
  20.  
  21. //5) this explains when to call getNumOfEvens() method.
  22. System.out.println("It is time to call getNumOfEvens() method. ");
  23. System.out.println("Once you call that method, it will return number of evens");
  24.  
  25. //6) actual calling getNumOfEvens() method by passing the array reference variable named list.
  26. System.out.println(getNumOfEvens(list));
  27.  
  28.  
  29. //7) this is how to call void method with this case.
  30. getNumOfEvensVoid(list);
  31. }
  32.  
  33. //1) make sure change the original method of getNumberOfEvens to static as below.
  34. //2) just add static between public and int.
  35. public static int getNumOfEvens( int[] nums) {
  36. int count = 0;
  37.  
  38. for(int i = 0; i < nums.length; i++)
  39. {
  40. if(nums[i] % 2 == 0)
  41. count++;
  42.  
  43. }
  44. return count;
  45. }
  46.  
  47. public static void getNumOfEvensVoid( int[] nums) {
  48. int count = 0;
  49.  
  50. for(int i = 0; i < nums.length; i++)
  51. {
  52. if(nums[i] % 2 == 0)
  53. count++;
  54.  
  55. }
  56. System.out.println("This arrays have " + count + " even numbers. ");
  57. }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement