Advertisement
Guest User

Untitled

a guest
Nov 9th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. package test5;
  2.  
  3. public class AverageMatch {
  4.  
  5. private static int[] student = {2, 3, 1, 7, 5};
  6.  
  7. private static String[] movies = {"movie1", "movie2"};
  8.  
  9. private static int[][] friends = {
  10. {1, 3, 4, 5, 7},
  11. {3, 5, 8, 6, 9},
  12. {6, 4, 1, 2, 3},
  13. {9, 7, 5, 3, 1},
  14. {8, 6, 4, 2, 1}
  15. };
  16.  
  17. /*
  18. int matches
  19.  
  20. for each entry in student
  21. for each row in friends
  22. for each column in friends
  23. if student entry matches friend[row][column]
  24. increment matches
  25. end if
  26. end for
  27. end for
  28. end for
  29.  
  30.  
  31. matches > 0 ?
  32. print average (matches / friends.length)
  33. else
  34. print no matches
  35. */
  36.  
  37. public static void main(String[] args) {
  38. int matches = 0;
  39. int average;
  40.  
  41. for (int i = 0; i < friends.length; i++) {
  42. matches += sharedPreferences(student, friends[i]);
  43. }
  44.  
  45. if (matches > 0) {
  46. average = matches / friends.length;
  47.  
  48. System.out.println("The five friends share on average " + average + " preferences with the student");
  49. } else {
  50. System.out.println("No matches");
  51. }
  52. }
  53.  
  54. public static int sharedPreferences(int[] studentPreferences, int[] friendPreferences) {
  55. int matches = 0;
  56.  
  57. for (int i = 0; i < studentPreferences.length; i++) {
  58. for (int j = 0; j < friendPreferences.length; j++) {
  59. if (studentPreferences[i] == friendPreferences[j]) {
  60. matches++;
  61. }
  62. }
  63. }
  64.  
  65. return matches;
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement