Sim0o0na

MovieRating

Jul 4th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class MovieRating {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         // 1. Read count of movies
  10.         int moviesCount = Integer.parseInt(scanner.nextLine());
  11.         // 2. Iterate movies
  12.         double lowestRating = Double.POSITIVE_INFINITY;
  13.         String lowestRatingMovieName = "";
  14.  
  15.         double highestRating = Double.NEGATIVE_INFINITY;
  16.         String highestRatingMovieName = "";
  17.         double ratingSum = 0;
  18.         for (int movie = 1; movie <= moviesCount; movie++) {
  19.             // 2.1. read name of movie
  20.             String movieName = scanner.nextLine();
  21.             // 2.2. read movie rating
  22.             double movieRating = Double.parseDouble(scanner.nextLine());
  23.             ratingSum += movieRating;
  24.             // 2.3. compare if rating is lowest
  25.             if (movieRating < lowestRating) {
  26.                 // 2.3.1. set lowest rating movie name
  27.                 lowestRatingMovieName = movieName;
  28.                 // 2.3.2. set lowest rating value
  29.                 lowestRating = movieRating;
  30.             }
  31.             // 2.3. compare if rating is highest
  32.             if (movieRating > highestRating) {
  33.                 // 2.3.1. set highest rating movie name
  34.                 highestRatingMovieName = movieName;
  35.                 // 2.3.2. set highest rating value
  36.                 highestRating = movieRating;
  37.             }
  38.         }
  39.  
  40.         // 3. Print output
  41.         System.out.printf("%s is with highest rating: %.1f%n",
  42.                 highestRatingMovieName, highestRating);
  43.         System.out.printf("%s is with lowest rating: %.1f%n",
  44.                 lowestRatingMovieName, lowestRating);
  45.         // 4. Calculate average rating
  46.         double averageRating = ratingSum / moviesCount;
  47.         // 5. Print average rating
  48.         System.out.printf("Average rating: %.1f", averageRating);
  49.     }
  50. }
Add Comment
Please, Sign In to add comment