LoslegenKostenlos loslegen

Don't try to change me

Imagine a three-player game of Twenty-One with your friends Alex, Chen, and Marta. Then imagine Marta dropping out of the game and your friend Umberto joining in her place. Note that player names are of type String, which is a sequence of Char in Scala. String is also commonly used in data-related tasks.

In this exercise, you will define immutable variables (val) then try to reassign them. You will encounter an error. Seeing this error message in a controlled setting will help you interpret this message and similar ones when you're coding on your own in the future.

Diese Übung ist Teil des Kurses

Introduction to Scala

Kurs anzeigen

Anleitung zur Übung

  • Define playerA, playerB, and playerC as immutable variables with the strings "Alex", "Chen", and "Marta", respectively.
  • Read the code that reassigns playerC to "Umberto". Click "Run Code" and observe the error.
  • Delete the code that reassigns playerC to "Umberto".
  • Change the original playerC variable definition from "Marta" to "Umberto".

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

// Define immutable variables for player names
___ playerA: ___ = ___
___ playerB: ___ = ___
___ playerC: ___ = ___

// Change playerC from Marta to Umberto
playerC = "Umberto"
Code bearbeiten und ausführen