Advertisement
myrdok123

P04MovieRatings

Apr 20th, 2024
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. package ExamPrep;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P04MovieRatings {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.  
  10.         int countMovies = Integer.parseInt(scanner.nextLine());
  11.  
  12.         double lowestRating = 11;
  13.         String movieWithLowestRating = "";
  14.  
  15.         double highestRating = 0;
  16.         String movieWithHighestRating = "";
  17.  
  18.         double sumRatings = 0;
  19.  
  20.         for (int i = 1; i <= countMovies ; i++) {
  21.  
  22.             String name = scanner.nextLine();
  23.             double rating = Double.parseDouble(scanner.nextLine());
  24.  
  25.             sumRatings += rating;
  26.  
  27.             if(rating > highestRating){
  28.                 movieWithHighestRating = name;
  29.                 highestRating = rating;
  30.             }
  31.  
  32.             if (rating < lowestRating){
  33.                 movieWithLowestRating = name;
  34.                 lowestRating = rating;
  35.             }
  36.         }
  37.  
  38.         double averageRating = sumRatings / countMovies;
  39.  
  40.         System.out.printf("%s is with highest rating: %.1f%n", movieWithHighestRating, highestRating);
  41.         System.out.printf("%s is with lowest rating: %.1f%n", movieWithLowestRating, lowestRating);
  42.         System.out.printf("Average rating: %.1f", averageRating);
  43.     }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement