if and else inside of a function
Since if expressions result in values, they can be the result of functions, which also result in values.
In this exercise, you'll create the body a function for your Twenty-One program called pointsToBust, which takes the current hand's point value as an input and calculates the the number of points remaining until 21. As a player, knowing how many additional points it takes to cause your current hand to bust will help you decide whether to hit or stay. The card variables you need and the bust function are already defined for you.
이 연습은 강의의 일부입니다
Introduction to Scala
연습 안내
- Write an
ifcondition: if the hand busts, make theInt0 the result of the function. - Write an
elsecondition: if the hand does not bust, subtracthandfrom 21 and make that difference the result of the function. - Call
pointsToBustwith a hand with the cardstenSpadesandfiveClubsas the argument.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
// Find the number of points that will cause a bust
def pointsToBust(hand: Int): Int = {
// If the hand is a bust, 0 points remain
___ ___
___
// Otherwise, calculate the difference between 21 and the current hand
___
___
}
// Test pointsToBust with 10♠ and 5♣
val myHandPointsToBust = ___
println(myHandPointsToBust)