#code that calculates the smallest of three numbers. Rewrite: instead of ? and : use if else

1 messages · Page 1 of 1 (latest)

sweet vergeBOT
#

<@&987246554085740594> please have a look, thanks.

tepid torrent
#

its interesting that u ask this way around

#

usually people know if-else very well and have to learn ?: first

#

and not the other way around

#
Foo foo = condition ? A : B;

is the same as

Foo foo;
if (condition) {
  foo = A;
} else {
  foo = B;
}

🙂

bronze abyss
#

it was the solution within the course but I never once saw that in the previous lessons but that´s the way the course is built sometimes you need something before it even gets discussed in the exercises

tepid torrent
#

its called the ternary operator and it does exactly what i just showed 🙂

#

"if condition, use this. else that"

#
Drink drink = isItHot() ? iceTea : hotTea;
bronze abyss
#

I set on that exercise two hours and tied to solve it with if else before I looked into the solution and there were those two symbols used which I like I already metioned never saw before 😂

bronze abyss
#

but there are a few conditions how can I describe those with just one condition bracket

tepid torrent
#

u can also nest ifs

#

or write multiple cases

#

theres so many ways to write this

bronze abyss
#

I feel like I went through all possible if else combinations within those two hours and after I attempted to rewrite the given solution

bronze abyss
tepid torrent
#

well. step back and try to first write the explicit solution. dont try to be overly clever on it

#

when is a the min element? when it is smaller than b and c

#
if (a < b && a < c) {
  return a;
}
#

there u go, first case

bronze abyss
#

I tried that as well

tepid torrent
#

now else if for b

#

then else for c and ur done

bronze abyss
#

but it ended in a error case one sec I think I still have it copied somewhere

tepid torrent
#

then understand and fix the error instead of throwing the code away and trying a totally different approach 🙂

#

u must have made a minor mistake only

bronze abyss
#

I tried to fix it several times but it said something about enum etc

tepid torrent
#

if u have the code still, we can take a look

bronze abyss
#

and after that I didn´t get any further unfortunately

#

one sec still searching

bronze abyss
#

if (a < b && a < c){
return a;
}
else if (b < a && b < c){
return b;
}
else{
return c;
}

}
#

It worked now will definetely take your advice to heart and try to stick to my one solution and try to figure it out until it gets accepted and not until I don´t understand the error message

#

Thanks a bunch! @tepid torrent

scenic tartan
#

what happens if a = b < c

bronze abyss
#

you mean a < b <c ?

scenic tartan
#

no

bronze abyss
#

was too quick

scenic tartan
#

if a and b are equal, and less than c

bronze abyss
#

then the conditions would be decribed with <=

#

instead of =

scenic tartan
#

but right now your method returns c

bronze abyss
#

<*

scenic tartan
#

which is wrong

bronze abyss
#

public class Solution {
public static int min(int a, int b, int c) {
//schreib hier deinen Code
if (a <= b && a <= c){
return a;
}
else if (b <= a && b <= c){
return b;
}
else{
return c;
}

#

now the system verified it with all the given conditions only cliqued run and not verify

#

thanls for bringing that part to my attention

#

@scenic tartan