So let's look at the solution for the guess the number application.
How would you build it?
Well, the thing is we need to ask the user for the number.
So guess the number I'm thinking of between 1 and 100 for example.
Okay.
Then we need to make sure that we actually have the number.
So we need to have a secret number that the computer has in mind as well as our own number.
So int user guess and I'm going to set that to zero.
So this will be the user guess.
And this will be the number that the computer has in mind that we have to guess.
Now we can make it random.
That would be an option as well.
But let's keep it simple for now.
So then what we can do is we can check if the user guess is not the same as the secret number.
We want to give the user the choice to enter his guess.
So here I'm going to say enter your guess and then we overwrite the user guess.
So we're going to say user guess and we need to pass whatever the user has entered because it is a string.
Right.
So now we're passing that into an int.
And that will give us the user guess.
And then we can check if the user guess was the same as the secret number again.
So if user guess is the same or in this case, what's more likely is that the user guess this number
is lower or higher than the secret number.
Okay, so I'm going to say if the user guesses lower I'm going to let the user know to lo try again.
Else if the guess was too high then I'm going to say too high.
Try again.
So CW two high try again.
And in all other cases we know that the user was correct.
So here we're going to say congratulations.
You guessed the number.
And maybe you guessed the number right.
Something like this okay.
And that's pretty much our software.
So what will happen now once the user did guess the number right.
This here will be executed.
And the user guess will not be not the secret number anymore.
So they will be the same.
And if they are the same then this while loop isn't executed anymore.
So that's the idea here.
So as long as this condition is true, which means the user guess is not the same number as the secret
number, then this while loop will be running as soon as they are the same.
This statement here is not true anymore, because we're using this exclamation mark here where it says
not equal secret number.
Okay, so let's run our application and see how it goes.
So there we are.
Think of a number between 1 and 100.
So I'm going to say five.
It says too low.
Try again.
Let me say 25.
Too low.
Try again.
Let's use 55.
Too high.
Try again okay.
Let's use 50.
Still too high.
Let's use 42.
And it says congratulations.
You guessed the number right.
So that's pretty much it.
So now you could add a counter which counts how many tries we needed.
And you could display the amount of tries here as well.
So how would we do that.
We would say int counter equals zero.
And each time that this code is run we're going to use the counter.
And once the user got it right we can add the counter.
We can say it took you plus counter plus tries.
And I think it's written like this okay so let me stop the application here.
So it says congratulations.
You guessed the number right.
It took you counter tries I'm not sure.
Maybe it's written with a Y.
I'm not sure.
Okay.
Guess the number.
I'm thinking between 1 and 100.
So let's do it again.
25, 50, 42.
And then it says congratulations.
You guessed the number, right?
It took you three tries.
Perfect.
And I'm going to have another empty space here.
But yeah that's pretty much our guess.
The number application.
Quite simple, quite straightforward but pretty cool.
Now you could go ahead and use a random number here.
We've seen how to use random before.
So if you want to try it yourself pause the video now and try it.
So I'm using the random random new random class.
So here creating an instance of the random class.
And then I'm setting the secret number here to be random dot next.
Between 1 and 101.
So I do it up here because I set the number once and now I won't know what the number is.
So let me actually use yeah let me run this and I will not know the number.
And let's see how many tries I need.
So five to low 50 to high 30 to low 35 to high okay.
32 is too low okay.
There we are.
So you guessed the number right.
It took you six tries.
So there you are.
Now you have a guess the number application.
That is each time going to be unique.
So each time the number is going to be different.
It's not going to be the same number every time.
So that's it for this little application we have built a guest number app with all the features that
we have learned so far, which is very cool.
So it contains creating a random number.
It contains data types like integers.
The console obviously we use while loops with a condition with a not equal sign we use incrementing.
We also use parsing as well as getting user input.
We are using if statements, else if statements and else statements and even some concatenation here.
And yeah, pretty much all the features that we have learned so far.
Not all, but most of them.
And I hope you enjoyed this little challenge.
I have another really cool project for you.
Ready?
So see you in the next video!