Bắt đầu ngayBắt đầu miễn phí

A simple while loop

A while loop is another control structure, like if/else. while lets us automate the repetition of instructions to make our code more readable and concise.

In this exercise, you will write a while loop that loops three times. Using an incrementing counter variable and if/else logic, you will print "winner" in the first iteration of the loop, "winner" again in the second iteration, and "chicken dinner" in the third and final iteration. This printed message won't be used in the Twenty-One program exactly, though it could be used in the context of congratulating a player if they win a round.

Bài tập này là một phần của khóa học

Introduction to Scala

Xem khóa học

Hướng dẫn bài tập

  • Define a counter variable, i, equal to zero.
  • Define the number of iterations for the while loop, numRepetitions, equal to three.
  • Fill out the first if clause so "winner" is printed in the first iteration of the loop, then "winner" again in the second iteration, and "chicken dinner" in the final iteration.
  • Increment the counter variable by one. If you don't do this, your code will run indefinitely and the console will crash.

Bài tập tương tác thực hành trực tiếp

Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.

// Define counter variable
___ i = ___

// Define the number of loop iterations
___ numRepetitions = ___

// Loop to print a message for winner of the round
___ (___ ___ ___) {
  if (___ < ___)
    println("winner")
  else
    println("chicken dinner")
  // Increment the counter variable
  ___
}
Chỉnh sửa và Chạy Mã