D.R D.R - 7 days ago 6
C# Question

How to reloop the game and ask something

In the advance i want to say thank u for taking the time to help me. Im a starter in C# and practise allot but im stuck here.

I made a program that calculates the temprature in different ways.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Excercise_7
{
class Program
{
static void Main(string[] args)
{
showBegin();
decide();
secondPlay();
showEnd();

}
// Decide code
static void decide() {
int choice;
string input;
input = Console.ReadLine();
int.TryParse(input, out choice);
if (choice == 1)
{
Console.WriteLine("");
celciusSystem();
}
else if (choice == 2)
{
Console.WriteLine("");
farenheitSystem();
}
else if (choice == 3)
{
Console.WriteLine("");
kelvinSystem();
}
else {
Console.WriteLine("");
Console.WriteLine("Make sure u write a number between 1 and 3! No text!");
Console.WriteLine("");
showBegin();
decide();
}
}
static void secondPlay()
{
int choice2;
string input;
Console.WriteLine("");
Console.WriteLine("Type 1 to play again, type 2 to close the application");
input = Console.ReadLine();
int.TryParse(input, out choice2);

}

// Begin code
static void showBegin()
{
Console.WriteLine("Welcom at the temperature calculator, pick the mode u prefer!");
Console.WriteLine("Press 1 for Celcius mode");
Console.WriteLine("Press 2 for Fahrenheit mode");
Console.WriteLine("Press 3 for Kelvin mode");
Console.WriteLine("Press the correct number and then hit the enter button!");
Console.WriteLine("");
}
// Temperature Calculators
static void celciusSystem()
{
Console.WriteLine("This is the Celsius calculator");
Console.Write("Enter the amount of celsius: ");

int celsius = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("");
Console.WriteLine("The temperature in Kelvin = {0}", celsius + 273);
Console.WriteLine("The temperature in Fahrenheit = {0}", celsius * 18 / 10 + 32);

}
static void farenheitSystem()
{
Console.WriteLine("This is the Fahrenheit calculator");
Console.Write("Enter the amount of Fahrenheit: ");

int Fahrenheit = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("");
Console.WriteLine("The temperature in Kelvin = {0}", (Fahrenheit + 459.67) / 1.8);
Console.WriteLine("The temperature in Celsius = {0}", (Fahrenheit - 32) / 1.8);

}

static void kelvinSystem()
{
Console.WriteLine("This is the Kelvin calculator");
Console.Write("Enter the amount of Kelvin: ");

int Kelvin = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("");
Console.WriteLine("The temperature in Fahrenheit = {0}", Kelvin * 1.8 - 459.67);
Console.WriteLine("The temperature in Celsius = {0}", Kelvin - 273.15);

}
// End code
static void showEnd()
{
Console.WriteLine("");
Console.WriteLine("We hoped u enjoyed our application!");
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
}


}
}


So now my question, I made a class called second play. Within that class i tried to make a kind a statement that would ask the user if he or she wanted to "play" again. So if they write 1 he should loop the game again.

I have tried it with this

if (choice2 == 1)
{
while (choice2 == 1) {
Console.WriteLine("");
showBegin();
decide();
}

}
else {
Console.WriteLine("");
Console.WriteLine("Cheers!!");
}
}


then he actually worked but not the way I wanted to cus if the user writes 1 it will loop the first thing all over again. So the user cant quit the game anymore.

I want the application to ask after every trail if he or she wants to play again by typing 1 or 2.

I hope I served enough information, and I hope someone knows the awnser to my qustion!

Answer

You already know how to read lines from the console, you also know how to use if to compare booleans, so why don't you just apply this and check if they wrote a 1 or 2?

private bool playAgain()
{
    int number = 2; //Invalid option: close game
    Int32.TryParse(Console.ReadLine(), out number);
    if (number == 1)
    {
        return true;
    }
    else if (number == 2)
    {
        return false;
    }
    else
    {
        Console.WriteLine("Unknown option, quitting!");
        return false;
    }
}

Then you just have to use that function to determine if they want to play again in a do-loop.

static void Main(string[] args)
{
    showBegin(); //Show begin sequence
    do
    {
    decide();   
    secondPlay();
    } while(playAgain()); //Play again if they want to
    showEnd(); //Show end sequence when they decided not to play

}

Pro tip: use a switch-case statement instead of multiple if-else statements.

Comments