Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test5;
- public class AverageMatch {
- private static int[] student = {2, 3, 1, 7, 5};
- private static String[] movies = {"movie1", "movie2"};
- private static int[][] friends = {
- {1, 3, 4, 5, 7},
- {3, 5, 8, 6, 9},
- {6, 4, 1, 2, 3},
- {9, 7, 5, 3, 1},
- {8, 6, 4, 2, 1}
- };
- /*
- int matches
- for each entry in student
- for each row in friends
- for each column in friends
- if student entry matches friend[row][column]
- increment matches
- end if
- end for
- end for
- end for
- matches > 0 ?
- print average (matches / friends.length)
- else
- print no matches
- */
- public static void main(String[] args) {
- int matches = 0;
- int average;
- for (int i = 0; i < friends.length; i++) {
- matches += sharedPreferences(student, friends[i]);
- }
- if (matches > 0) {
- average = matches / friends.length;
- System.out.println("The five friends share on average " + average + " preferences with the student");
- } else {
- System.out.println("No matches");
- }
- }
- public static int sharedPreferences(int[] studentPreferences, int[] friendPreferences) {
- int matches = 0;
- for (int i = 0; i < studentPreferences.length; i++) {
- for (int j = 0; j < friendPreferences.length; j++) {
- if (studentPreferences[i] == friendPreferences[j]) {
- matches++;
- }
- }
- }
- return matches;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement