开始使用免费开始使用

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 if condition: if the hand busts, make the Int 0 the result of the function.
  • Write an else condition: if the hand does not bust, subtract hand from 21 and make that difference the result of the function.
  • Call pointsToBust with a hand with the cards tenSpades and fiveClubs as 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)
编辑并运行代码