Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class MovieRating {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // 1. Read count of movies
- int moviesCount = Integer.parseInt(scanner.nextLine());
- // 2. Iterate movies
- double lowestRating = Double.POSITIVE_INFINITY;
- String lowestRatingMovieName = "";
- double highestRating = Double.NEGATIVE_INFINITY;
- String highestRatingMovieName = "";
- double ratingSum = 0;
- for (int movie = 1; movie <= moviesCount; movie++) {
- // 2.1. read name of movie
- String movieName = scanner.nextLine();
- // 2.2. read movie rating
- double movieRating = Double.parseDouble(scanner.nextLine());
- ratingSum += movieRating;
- // 2.3. compare if rating is lowest
- if (movieRating < lowestRating) {
- // 2.3.1. set lowest rating movie name
- lowestRatingMovieName = movieName;
- // 2.3.2. set lowest rating value
- lowestRating = movieRating;
- }
- // 2.3. compare if rating is highest
- if (movieRating > highestRating) {
- // 2.3.1. set highest rating movie name
- highestRatingMovieName = movieName;
- // 2.3.2. set highest rating value
- highestRating = movieRating;
- }
- }
- // 3. Print output
- System.out.printf("%s is with highest rating: %.1f%n",
- highestRatingMovieName, highestRating);
- System.out.printf("%s is with lowest rating: %.1f%n",
- lowestRatingMovieName, lowestRating);
- // 4. Calculate average rating
- double averageRating = ratingSum / moviesCount;
- // 5. Print average rating
- System.out.printf("Average rating: %.1f", averageRating);
- }
- }
Add Comment
Please, Sign In to add comment