S
S
Stich252022-02-06 15:41:50
Java
Stich25, 2022-02-06 15:41:50

There are 2 code options. In the 2nd variant, at a certain position of the return operator in the if-else branch, the program generates an error, why?

https://pastebin.com/RTmAqef0 The 1st version of the code is working. Pay attention to the last }else{ return film3;} inside the block.
https://pastebin.com/Wt8yihQD 2nd version of the code and here the program gives a missing return statement error. I already understood that if the return statement is written outside the block }else{ } return film3; or remove the else altogether and call the return statement at the end of the method, everything works, but I don’t understand why this happens.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Danya Kozlovskiy, 2022-02-06
@Stich25

Most likely, the compiler swears that you do not return any value in all event variants, for example, in the code below, if the condition is met if(income1>income2), but the nested in it is not executed if(income1>income3), then the function simply will not return anything, because the else block for the nested if is missing .

if (income1>income2) {
            if (income1>income3) { 
                return film1;  
            }

O
Orkhan, 2022-02-06
Hasanly @azerphoenix

I fully agree with what my colleagues have written.
From myself I will add that your code can be somewhat simplified. If you have to compare not 2 or 3 films, but let's say 100 pieces, then you will simply go crazy for each of them to select a combination and build if else branches. So, there must be another "general" solution.
You can check out one of the solutions using which you can compare as many films as you like and without any if .. else:

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        // Создаем фильмы
        Cinema cinema1 = new Cinema("Титаник", 2194);
        Cinema cinema2 = new Cinema("Аватар", 2810);
        Cinema cinema3 = new Cinema("Тёмный рыцарь", 1084);
        Cinema[] cinemas = new Cinema[]{cinema1, cinema2, cinema3};
        // Находим фильм с большим доходом
        String cinemaTitle = findHighestGrossingFilm(cinemas);
        System.out.println(cinemaTitle);
    }

    /**
     * Метод находит фильм с большим доходом
     * @param cinemas массив фильмов для сравнения
     * @return название фильма с большим доходом
     */
    public static String findHighestGrossingFilm(Cinema ... cinemas) {
        Cinema cinemaWithMaxIncome = Arrays.stream(cinemas).max(Cinema::compareTo).get();
        return cinemaWithMaxIncome.name;
    }
}

class Cinema implements Comparable<Cinema> {

    String name;
    int income;

    public Cinema(String name, int income) {
        this.name = name;
        this.income = income;
    }

    // Имплементируем интерфейс Comparable и сравниваем income объектов
    @Override
    public int compareTo(Cinema c) {
        return Integer.compare(this.income, c.income);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question