Jak większość udało mi się zrobić pół zadania (porównywanie a z b) ale zatrzymałam się przy użyciu jednej metody w drugiej.
Z lektury innych pytań wiem, jakie jest rozwiązanie - ale niestety go nie rozumiem.
Mogłabym prosić o wytłumaczenie?
{
return min(min(min(a, b), c), d);
}
Proszę o wytłumaczenie rozwiązania zadania
Dyskutowane
Komentarze (1)
- Popularne
- Najnowsze
- Najstarsze
Musisz się zalogować, aby dodać komentarz
Thomas
29 lutego, 11:28
That's just one possible solution. Easier to understand at the beginning would be to eg. get the min of a and b, then the min of c and d and last but not least you find the min of the results of the two previous method calls. That's pretty much the same as done in your code snippet above.
if you now don't call the last min method using n and m but instead the method calls you used to get these values then your code will look like
That's doing the same but is shorter but not that easy to read an understand. Your code snippet is similar to what I showed you here. Just the methods are differently sorted into the outer method call.
This is working because the compiler knows about nested method calls. Before it actually invokes the method, the compiler evaluates the method parameter.
Let's look at a simpler version min(min(a, b), 1);
To be able to invoke the outer min method, the compiler needs to know the result of min(a, b). So it evaluates that method first and uses its result to invoke the outer method. So if min(a, b) results in 5, then the compiler will invoke min (5, 1) in the end. +1