Alex Craig Alex Craig - 3 months ago 22
C# Question

Visual Studio C# Redirect to a game over screen

i am a beginner in C# and i have to hand in an assignment soon and i need help with one thing. also i am using Console Application

In the game you have to go through different rooms and kill "zombies" so far it will go to unlimited rooms but i want it to stop once the user has done the 25th room, i have written code that should work but i don't know why it is not working. here is the code i used

any help appreciated.

{
if (room1 >25)
Console.WriteLine("");
Console.WriteLine("Congratulations! You made it passed all 25 Rooms. You Defeated all the mobs");
Console.ReadLine();
Console.WriteLine("Press enter to Quit...");
}

Answer

You need curly braces after your if statement, like so.

if (room1 >25)
{
    Console.WriteLine("");
    Console.WriteLine("Congratulations! You made it passed all 25 Rooms. You Defeated all the mobs");
    Console.ReadLine();
    Console.WriteLine("Press enter to Quit...");
}

Without them what you're saying is

if room1>25, then write "".
Regardless of room number, do everything else.

That's because in C#, you CAN have an if statement without curly braces, but in that case it MUST be only one line long. So your code above is actually equivalent to this:

if (room1 >25)
{
    Console.WriteLine("");
}
    Console.WriteLine("Congratulations! You made it passed all 25 Rooms. You Defeated all the mobs");
    Console.ReadLine();
    Console.WriteLine("Press enter to Quit...");