시작하기무료로 시작하기

Define mutable variables (var)

Scala has two kinds of variables: val and var. var are mutable and can be reassigned.

In Twenty-One, an ace is like a var, as it can be worth 1 or 11 points depending on the player's choice (as opposed to cards 2 through king, which have a constant point value throughout the game).

In this exercise, you will define each ace as a var of type Int set to a default value of 1 point. This time around, you'll leverage Scala's type inference (its ability to guess types based on supplied values) and omit the type annotation.

이 연습은 강의의 일부입니다

Introduction to Scala

강의 보기

연습 안내

  • Define four mutable variables, all of type Int and equal to 1: aceClubs, aceDiamonds, aceHearts, aceSpades.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

// Define mutable variables for all aces
코드 편집 및 실행