Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: Number Guessing Simulation | While and Do-While Loops: Control and Flexibility
Quizzes & Challenges
Quizzes
Challenges
/
C# Loops Practice

bookChallenge: Number Guessing Simulation

In this challenge, you will apply your understanding of do-while loops by simulating a simple number guessing game. The key feature of this task is that the guessing loop must always execute at least once, regardless of whether the guess is correct on the first try. This ensures you gain practical experience using a do-while loop, which guarantees the loop body runs before any condition is checked.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930
using System; namespace ConsoleApp { public class Program { public static void Main() { int secretNumber = 7; int guess = 7; // The guess is correct on the first try bool guessedCorrectly = false; do { Console.WriteLine("Guessing..."); if (guess == secretNumber) { guessedCorrectly = true; Console.WriteLine("Correct guess!"); } else { Console.WriteLine("Wrong guess."); // In a real game, the guess could be updated here } } while (!guessedCorrectly); } } }
Tehtävä

Swipe to start coding

Simulate a number guessing game using a do-while loop. The loop should always run at least once, even if the first guess is correct.

  • Use the guesses array to represent a series of guesses.
  • Compare each guess to the secretNumber.
  • If a guess matches the secret number, set guessedCorrectly to true.
  • After each guess, increment the index variable.
  • Print "You guessed it!" if the guess is correct, or "Try again." if not.

Ratkaisu

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 4
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Can you explain how a do-while loop works in this context?

Can you provide an example of a number guessing game using a do-while loop?

What are some common mistakes to avoid when using do-while loops?

close

Awesome!

Completion rate improved to 5.56

bookChallenge: Number Guessing Simulation

Pyyhkäise näyttääksesi valikon

In this challenge, you will apply your understanding of do-while loops by simulating a simple number guessing game. The key feature of this task is that the guessing loop must always execute at least once, regardless of whether the guess is correct on the first try. This ensures you gain practical experience using a do-while loop, which guarantees the loop body runs before any condition is checked.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930
using System; namespace ConsoleApp { public class Program { public static void Main() { int secretNumber = 7; int guess = 7; // The guess is correct on the first try bool guessedCorrectly = false; do { Console.WriteLine("Guessing..."); if (guess == secretNumber) { guessedCorrectly = true; Console.WriteLine("Correct guess!"); } else { Console.WriteLine("Wrong guess."); // In a real game, the guess could be updated here } } while (!guessedCorrectly); } } }
Tehtävä

Swipe to start coding

Simulate a number guessing game using a do-while loop. The loop should always run at least once, even if the first guess is correct.

  • Use the guesses array to represent a series of guesses.
  • Compare each guess to the secretNumber.
  • If a guess matches the secret number, set guessedCorrectly to true.
  • After each guess, increment the index variable.
  • Print "You guessed it!" if the guess is correct, or "Try again." if not.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 4
single

single

some-alt