I want to return to the main menu not restarting the program all over again to change the type of conversion .. Any help please ? I will be thankful if somebody here helped me ..
using System;
namespace Assignment_1_Csharp
{
internal class Program
{
static void Main(string[] args)
{ int choice;
double val,EGP;
Console.WriteLine("Enter your Choice :\n 1- Dollar to EGP \n 2- EGP to Dollar ");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
double dollar;
Console.Write("Enter the Dollar Amount :");
dollar = double.Parse(Console.ReadLine());
Console.Write("Enter the Dollar Exchange Rate :");
val = double.Parse(Console.ReadLine());
EGP = dollar * val;
Console.WriteLine("{0} Dollar Equals {1} EGP", dollar, EGP);
break;
case 2:
Console.Write("Enter the EGP Amount :");
EGP = double.Parse(Console.ReadLine());
Console.Write("Enter the Dollar Exchange Rate :");
val = double.Parse(Console.ReadLine());
dollar = EGP / val;
Console.WriteLine("{0} EGP Equals {1} Dollars", EGP, dollar);
break;
}
Console.ReadLine();
}
}
}
CodePudding user response:
One way to solve this is using a do-while loop. The following code will run through the choice selection and conversion and then ask user to continue. If the selection is "y" then it will ask the user to choose again otherwise exit the main method.
public static void Main(string[] args)
{
int choice;
double val, EGP;
string userSelection = "y";
do
{
Console.WriteLine("Enter your Choice :\n 1- Dollar to EGP \n 2- EGP to Dollar ");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
double dollar;
Console.Write("Enter the Dollar Amount :");
dollar = double.Parse(Console.ReadLine());
Console.Write("Enter the Dollar Exchange Rate :");
val = double.Parse(Console.ReadLine());
EGP = dollar * val;
Console.WriteLine("{0} Dollar Equals {1} EGP", dollar, EGP);
break;
case 2:
Console.Write("Enter the EGP Amount :");
EGP = double.Parse(Console.ReadLine());
Console.Write("Enter the Dollar Exchange Rate :");
val = double.Parse(Console.ReadLine());
dollar = EGP / val;
Console.WriteLine("{0} EGP Equals {1} Dollars", EGP, dollar);
break;
}
Console.WriteLine("Enter Y to choose again...");
userSelection = Console.ReadLine();
}
while (userSelection.ToLower() == "y");
}
You can change the text messages and selection to anything you want.
CodePudding user response:
After Console.Readline()
, just add Main(null)
.