OK, so here we will add the "return n;" and while 'n' is not how we want it to be,
while 'n' is negative or null, then we will repeat, we will loop.
We will first display the message, so, this line, then we will read the answer,
so we will add this line here.
Concerning the variable declarations, we have to declare the scanner
and declare 'n', all these lines here, outside the loop since
we don't need to repeat the scanner in the loop and we only need to
declare 'n' once.
So basically, this gives the following code: we will start by declaring
the scanner, once and for all, for the keyboard input; then we will declare
the variable 'n'. It must be declared outside of the loop since we will
use it here to return it. We also use it in the test, which is outside of the scope of the loop's body.
So we declare this variable and initialize it to a value which makes
sense, let's say 0. Then, we write the loop bloc here,
the bloc which we want to repeat, in which we will display
the message which asks the user to enter a number. Here, we can even
specify our intentions exactly so that the user knows what to do, we can say
that we want a strictly positive number. Then, we read this number into 'n'
from the keyboard and so we loop while we haven't received the right answer,
while 'n' is negative.
This is how we could have corrected the error of the missing "return"
when 'n' was equal to or lesser than 0.
Let's now present a few specific examples of methods; starting with
methods without return types.
I must first remind you that a method in the Java sense has nothing
to do with a mathematical function. A method in Java is simply a piece
of code that we reserved to be able to use it, to avoid repetition,
and in this sense, this piece of code does not necessarily need to return
anything to the rest of the program.
If this is the case, then we will specify the fact that the method returns nothing
by using a specific return type, the "void" type, to indicate
that we will not return anything to the rest of the program.
In such a case, the "return" instruction is now optional. We can absolutely
include no return statement at all in the method's body. If we really need one,
for example in an "if" statement, in order to stop the execution of the
body at a precise moment, then we will be able to use a return statement.
For example, let's take a method which I will call "afficheRacine" [TN: means "displaySquareRoot"]
and whose job is to display the square root of a double-type parameter received
here and named "a".
If 'a' is negative, then as you know, we cannot compute the square root and thus
there is nothing to display. So in this case, if the condition for 'a' to be negative
is true, then the "if" will go to this return statement
-- there is only a single "return" here -- and this will thus end the execution of the method.