♫Woozy♫
❔ Number guessing game.
I'm trying to make a program that generates a random number between 1-100, let's the user guess till they get the correct number, tells the use if they guessed too high/low and also let's them know if they guess correctly and if so, tell's them how many guesses it took. But I want to make it more complicated by letting the user know when they are close to the number.
This is how my code looks so far:
using System.Diagnostics.CodeAnalysis;
Random random = new Random();
bool playAgain = true;
int min = 1;
int max = 101;
int guess;
int number;
int guesses;
while (playAgain)
{
guess = 0;
guesses = 0;
number = random.Next(min, max);
while (guess != number)
{
Console.WriteLine("Try to guess the randomly generated number " + min + "-" + max + ":");
guess = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Guess: " + guess);
if (guess < number)
{
Console.WriteLine("Number " + guess + " is too low...");
}
else if (guess > number)
{
Console.WriteLine("Number " + guess + " is too high!");
}
guesses++;
}
Console.WriteLine("You won! Good job!");
Console.WriteLine("Total guesses; " + guesses);
}
55 replies
How do I write text, then a int, then more text in the same line?
Trying to have it print "You have sum years left till pension."
Console.WriteLine("What's your name?");
string name = Console.ReadLine();
Console.WriteLine($"Hello {name}, how old are you?");
string input = Console.ReadLine();
int num1 = int.Parse(input );
int num2 = 65;
int sum = num2 - num1;
Console.WriteLine("You have " + sum " years left till pension");
9 replies