Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video, we'll explain the solution to the challenge.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Welcome back, how did it go?
0:00
It's okay if you weren't
able to complete every TODO.
0:02
When you're learning something new,
it's not unusual to struggle.
0:05
Let's walk through how I
implemented each improvement.
0:09
For the first task, we need to remove
the keepGoing variable, And replace
0:12
the reference to that variable in the
while loop condition with a value true.
0:18
This will cause the while loop to
execute until we break out of it.
0:24
For the next task,
0:30
we need to update the Console.Write method
call's argument to the string literal.
0:31
Enter a number between 1 and 1000.
0:37
Next, we need to force
the user's provided values for
0:42
the entry variable to lower case letters.
0:44
We can do that with a call to
the string ToLower method.
0:47
Forcing the user's provided values to
lower case will guarantee that our
0:52
equality comparison to the string
literal quit all in lower case.
0:56
Will always evaluate to true,
1:00
regardless if the user used
any capital letters or not.
1:02
Inside of the if statement we
need to replace this reference
1:06
to the keepGoing variable
with a break statement.
1:09
The break statement will cause the while
loop to immediately terminate and
1:13
execute the next line of
code after the wile loop.
1:18
Which in our case is this
call to the Console.WriteLine
1:21
method to display the text Goodbye!.
1:25
Now that we are breaking out the while
loop with this break statement
1:27
we no longer need the else statement.
1:30
So let's remove it,
1:33
Let's add a blank line there, then fix the
indication of the code by selecting it and
1:43
holding down Shift and pressing tab once.
1:49
For our next task we need to
add a try catch statement in
1:53
order to catch FormatException exceptions.
1:55
Let's start with adding try
followed by a set of curly braces.
1:59
Then catch, and a set of parentheses
followed by another set of curly braces.
2:05
Inside of the parentheses,
2:12
we need to specify the type of
exception that we want to catch.
2:14
We'll use (FormatException).
2:17
Which is the exception that'll be
thrown if the users provided value
2:21
can't be parsed to a number.
2:25
Then inside of the catch block, we want to
display a message to the user letting them
2:27
know that their provided value can't be
parsed to a number, so Console.writeLine.
2:33
Then for
the message let's display their provided
2:39
value followed by
the text is not a number.
2:43
Let's surround the user's provided
value with a set of single quotes
2:49
to help set it apart from
the rest of the message.
2:53
So, another string literal
followed by a plus sign
2:57
then inside these first set
of quotes add a single quote.
3:02
Then after the entry variable at
the beginning of the next string literal,
3:07
add another single quote.
3:12
Now let's move the rest of the code inside
of the while loop into the try block.
3:14
I put my cursor on the first line,
hold the Shift key down and press the down
3:19
arrow key until I've selected all
of the code that I want to move.
3:23
Then I'll press Ctrl + X or Cmd + X on
the Mac to cut selection to the clipboard.
3:27
Move my cursor to the beginning of the
line that contains the try block closing
3:36
curly brace and press Ctrl+V or Cmd+V
on the Mac to paste from the clipboard.
3:41
To fix the indentation of the code,
select the code and press the Tab key.
3:46
Next, we need to replace the int datatype
with a datatype that will allow for
3:55
fractional values like 2.5 or 5.75.
3:59
To do that we can switch to
using the double datatype.
4:04
Now that we're using the double
data type for the number variable.
4:11
We need to update the result
variable to be a double as well.
4:14
This is necessary because a double
multiplied by a double results in another
4:23
double, not an integer.
4:28
For our next two tasks, we need to
validate the user's provided value.
4:33
We have two validations to implement, so
I'll start with adding two if statements.
4:37
If and a set of parenthesis followed
by a set of curly braces, then again if
4:44
followed by a set of parenthesis
followed by another set of curly braces.
4:49
For the first validation,
4:56
you want to check if the user has entered
a value less than or equal to zero.
4:58
So let's compare the number
variable to the literal value 0.
5:03
For the comparison operator,
we can use less than or equal to.
5:07
If our expression evaluates to true,
we need to display a message to the user.
5:16
Let's add a call to
the Console.WriteLine method, for
5:21
the message text, I'll copy and
paste the text from this code comment.
5:25
For the second validation,
5:38
we want to check if the user has
entered a number greater than 1,000.
5:39
For our expression,
5:42
let's compare the number variable
to the literal value 1,000.
5:43
And for the comparison operator,
we can use greater than.
5:48
For our message, let's add another
call to the Console.WriteLine method,
5:54
and copy and
paste the text from the code comment.
5:59
There is one more thing we need to
do to finish up our validations.
6:09
If either of our if statements evaluates
to true, we can skip executing
6:13
the rest of the code in the while loop and
continue execution with the next loop.
6:18
We could do this by adding
a continuous statement
6:23
just after each of the ConsoleWriteLine
method calls inside of the if statements.
6:25
Here is the first, continue and
the second continue.
6:31
Now, if the user enters an invalid
value they'll see one of our
6:39
validation messages and
then be prompted to enter another value.
6:43
Before we test our solution,
let's make one more change to our code.
6:47
Notice that our validations
are mutually exclusive,
6:51
meaning that they'll never
both evaluate to true.
6:55
Because of this we can rewrite our two
independent if statements into an if else
6:58
if statement.
7:03
To be clear, this change won't
affect the behavior of our program.
7:07
But it does help clarify
the intent of our code,
7:12
which is something that we as
developers should always strive for.
7:15
All right,
we've completed all of the tasks.
7:19
Save the file by pressing Ctrl+S,
or Cmd+S on the Mac or
7:22
select the File>Save menu item.
7:25
Now let's compile and run our program.
7:30
Show the console by selecting
the View > Show Console menu item.
7:33
Run the command mcs Program.cs
to compile our program.
7:39
Now we can run the program using
the command mono space Program.exe.
7:45
Here's a prompt asking me for a number,
7:53
I'll enter a whole number say 3 and
there is my number squared.
7:56
3 multiplied by itself is equal to 9.
8:01
Let's try another number,
this time a fractional number 2.5.
8:04
2.5 multiplied by itself is equal to 6.25.
8:09
Now let's try entering
some random text and
8:14
we see a message letting us
know that asdf is not a number.
8:18
And we're prompted to
enter another number.
8:24
Let's try entering zero and
we see the first validation message.
8:26
Let's try a number less than zero.
8:31
And we see the first
validation message again.
8:32
Now let's try a number greater than 1000.
8:36
And we see the second validation message.
8:40
Now let's type quit, all in capital
letters and the program exits.
8:43
Great job completing
this practice session.
8:49
Implementing all these improvements really
helps to improve the overall quality of
8:52
the program.
8:56
Thanks for practicing with me,
and we'll see you next time.
8:57
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up