Onwards, the part two in this lesson you're going to get, these are two, three numbers and you're
going to make a function that checks if the user won or lost.
We're going to start using the familiar scanner to get user input, so by virtue of just writing a scanner
and choosing the first option, it's going to auto import scanner and allow me to use it.
Now, I'm going to set a variable scan equal to a new instance of scanner that receives input from the
system system in.
And before we forget, let's add scandal close to the end of the code.
The user needs to enter three numbers to start the game off, so I'm going to start by removing the
last three print line statements and before rolling the dice and ask the user to enter three whole numbers
between one and six print, enter three numbers between one and six.
OK, now we're going to add three, scan the next integer methods to pick up three numbers.
Think we're ready to run our code?
Enter three numbers for two and three, and we're looking good, and if you remember from our previous
discussion, it doesn't matter how much space you put in between the three numbers.
Scandal next and skips the delimiter, it skips the white space and it reads the next input, even if
you put an entire line of space between the three members Scandi next and skips all the white space
and reads the next input.
All of these methods skip the delimiter and read the next input.
Rescanned Eskandar next line just reads the entire line.
In any case, this table is inside your cheek.
It's.
Now, what if the user enters a no outside the range of one to six, if either of the three numbers
then or is not between one and six, we need to exit the game and hope this made you think of the or
operator such that we're going to check if the first number is less than one.
Or if the second number is less than one.
Or if the third number is less than one.
If either of these numbers is less than one, we're going to print numbers cannot be less than one.
Shutting the game down.
And we're going to use system that exit zero to shut the application down.
And now, if none of the numbers are less than the minimum value, we need to check if either one of
them exceeds the maximum value.
So we're going to check if the first number is bigger than six or if the second number is bigger than
six.
Or if the third number is bigger than six.
Then we're going to print.
Numbers cannot be higher than six, shutting the game down.
And once again, we're going to use system that exit zero to shut the application down.
OK, we're ready to run our code.
Let's enter a number outside the range, let's say negative two, and we'll put three and five.
Numbers cannot be less than one shunning the game down, the user entered a bad number.
So we shut the program down.
The first condition is designed to check if any of the numbers are less than one.
The first number is less than one.
So the entire condition is true and that runs the system, that exit code.
What if the user enters a number above six?
Let's try it out.
I'm going to put six, three and seven.
And numbers cannot be higher than six, shutting the game down, the last number is outside the range,
so the program shuts down.
Our trio of numbers is going to get past the first condition because none of them are less than one.
But the second condition is designed to check if any of the numbers are higher than six, since the
third number is higher than six.
The condition is true and a system that exit zero runs shutting the application down.
Now, if the user follows the rules, none of the statements should run and we should get to this part
of the code.
In that case, you can add all the numbers and add all the dice rolls.
So I'm going to say into some of numbers is equal to all three numbers that you chose.
And we'll say into some of roles, some of Dyce roles.
Is going to equal all of the dice rolls.
Then I'm going to print.
The day some.
Is equal to the sum of days, rose.
And the number some.
Is equal to the sum of numbers that you chose.
OK, we can test our code once more.
I'm going to make sure to follow the rules this time and enter three numbers between one and six days,
some 14, no sum equals 10.
All right, we're doing well so far.
And since none of the numbers are smaller than one or bigger than six, Java skips the statements.
Then the code adds all the numbers and stories.
The result in some of numbers, the code adds every dice roll and it stores the result in some of dice
rolls and our print statement runs cool.
OK, so now we're going to wrap up the game by checking if the user won.
I just want to remind you of what the rules are, the these are wins if the sum of dice rolls is smaller
than the sum of numbers that they choose and with an emphasis on the end, the difference between the
two numbers is less than three.
So if the user chooses four, three and five and happens to roll three, four, and for the user would
win because the sum of dice rolls is smaller than the sum of numbers.
And in this case, the difference between the two numbers is less than three.
So I think we're ready to define this task in a function.
It's going to be public.
I'm going to make it void for now, I'm going to address that in a second and I'll name the function,
check one, because it's going to check if the user one and this function is going to take two parameters,
it's going to take the sum of dice rolls.
And the sum of numbers.
And according to the rules, the user wins if the numbers they choose are bigger than the dice rolls.
That is, if no some is bigger than dice some.
And assuming this first comparison is true, such that the numbers you chose are indeed bigger than
the dice rolled.
We need to make sure that the difference between the two sums, some of numbers.
Minus some of dice rolls.
Is less than three.
If both comparisons are true, we're going to print.
Congrats.
You win.
Otherwise.
Sorry, Youlus.
The very last step is to call the function and pass the twosomes as arguments, so we're going to check
if the user one check when by passing in the sum of dice rolls and the sum of numbers that they chose.
I think we're ready to run our code.
I'm going to enter three numbers and dang it, I lost the numbers, I chose add up to 12, but the dice
rolls, whatever they are, they added up to 13.
So the first comparison was false, meaning that the entire condition became false.
And the Elkader runs.
Now, here's another scenario that could have happened as well.
We could have called the check win function and let's assume it passes in a December four and then no,
some of 12.
In this case, we have a higher number, some.
But the if statement is false because the difference between the numbers is higher than three.
So once again, the statement would run and I would lose the game.
But so far I'm really happy with how this game is turning out.
Try your luck.
You might win a few times.
Now, I know it might seem like we're done, but we're not actually done yet.
There's one thing that's really bothering me, Chuck, when it's void, when it shouldn't be a function,
should only perform its intended task and nothing more.
Chuck, when should only check if the user won or not?
It should not bear the responsibility of printing messages to the user.
In that case, I should just rename the function to check when and print random messages.
But we're definitely not doing that.
You might be saying, hey, Erin, it's not calculating anything, so it should be void.
Be careful.
A calculation doesn't have to involve numbers.
I like to think of a calculation as it uses some logic to come up with a value.
This condition, in some ways, it's a calculation.
It uses the logical operator and to perform two comparisons.
And the result is a boolean, true or false.
So it makes sense to just return the boolean that it computed because the Boolean is a result of your
calculation.
So I'm going to change the return type of check into to Boolean.
And then I'm going to return the result of this condition.
In this case, Chuck Wen is performing its intended task and nothing more, it's returning a boolean
that indicates whether the user won or lost and now inside mean the function call is going to hold this
return value.
So we need to make an if statement that checks if that return value is true, if the result from calling
check wen.
What these arguments end up being true.
We're going to print.
Congrats, you win.
Sorry, Youlus.
And with this final line of code, I like to proudly announce that we're all done, let's run our code.
And there you go, I won and this is also much better.
I like that we get the same output and that Szechwan is only performing its intended task this time.
The arguments being passed in are 12 and 13.
Both comparisons are true because my number is bigger than the sum of dice rolls and the difference
is smaller than three, which means the entire condition is true.
And that boolean result is a return to the function.
Call and send out the statement which holds the function call is going to be true.
The code inside runs and I win the game.
I'm going to try and run the code now so that I can lose.
I guess I'm on a roll.
Let's try again.
I guess today's my lucky day, I was going to try to rerun this and finally I lose.
This time around, Chetwyn receives a copy of these values as parameters, it performs the two comparisons,
and the resulting bullion from this condition is going to be false because although my numbers sum is
greater than the Decem, the difference is bigger than three.
So it returns false to the function call, which means the statement which holds my function call is
going to be false and the false code is going to run, in which case I lose once again.
All right.
It seems that we built a pretty robust game.
Congratulations, you built your first Java game.
The first part of this code prompts the user to enter three values, the second part of the code generates
three random values.
Then you took the sum of each and finally check one receives each sum and returns the result in the
form of a boolean.
And we use that boolean to determine if the user won or lost, that's all.
Not only did you learn about functions, but we gave them some pretty complex functionality.
The next challenge is coming up, so I hope you're ready if you ever have any questions, just ask me
on this court.
I'm always there.