-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
38 lines (32 loc) · 1.27 KB
/
Program.cs
File metadata and controls
38 lines (32 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
class Program {
static void Main() {
// Simple Number Guessing Game
Random random = new Random();
int numberToGuess = random.Next(1, 101); // Number between 1 and 100
int userGuess = 0;
int numberOfTries = 0;
Console.WriteLine("Welcome to the Number Guessing Game!");
Console.WriteLine("I have selected a number between 1 and 100. Can you guess it?");
// Loop until the user guesses the correct number
while (userGuess != numberToGuess) {
Console.Write("Enter your guess: ");
// Read user input
string input = Console.ReadLine();
// Validate input
if (!int.TryParse(input, out userGuess)) {
Console.WriteLine("Please enter a valid integer.");
continue;
}
numberOfTries++;
// Condition to check the user's guess
if (userGuess < numberToGuess) {
Console.WriteLine("Too low! Try again.");
} else if (userGuess > numberToGuess) {
Console.WriteLine("Too high! Try again.");
} else {
Console.WriteLine($"Correct! You've guessed the number in {numberOfTries} tries.");
}
}
}
}