我已經編寫了一個相當簡單的程序,該程序顯示選項菜單並根據用戶輸入執行計算。我想使程序運行,以便詢問用戶是否希望繼續返回菜單或退出程序。我在想它可能需要某種循環,但我不確定如何實現它。如何讓用戶選擇繼續執行程序還是退出程序(C#)
這裏是我的代碼
using System;
namespace WarmUpCalculations
{
class Program
{
static void Main(string[] args)
{
Console.Clear();
Console.WriteLine("Welcome to the Chemistry Formula Calculator!\n\n\n");
Console.WriteLine("Press 1 for the Density Calculator");
Console.WriteLine("Press 2 for the Moles Calculator");
Console.WriteLine("Press 3 for the Energy of a Wave Calculator");
Console.WriteLine("Press 4 for the Ideal Gas Law Calculator\n\n");
Console.WriteLine("Please enter a Number from the Options above");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
DensityCalculator();
break;
case "2":
MolesCalculator();
break;
case "3":
EnergyOfWaveCalculator();
break;
case "4":
IdealGasLawCalculator();
break;
}
}
static void DensityCalculator()
{
Console.Clear();
Console.WriteLine("Density Calaculator\n\n");
Console.WriteLine("Will this be for Grams or Kilograms?");
Console.WriteLine("Type 'g' for Grams or 'kg' for Kilograms");
string unitMass = Console.ReadLine();
Console.WriteLine("Please Enter Your Mass");
Decimal Mass = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Enter units for Volume");
string unitVolume = Console.ReadLine();
Console.WriteLine("Please Enter Your Volume");
Decimal Volume = Convert.ToDecimal(Console.ReadLine());
Decimal Density = Mass/Volume;
Math.Round(Density, 4);
Console.Clear();
Console.WriteLine("Moles Calaculator\n\n");
Console.Write("Your Density is ");
Console.Write(Density);
Console.Write(unitMass);
Console.Write("/");
Console.WriteLine(unitVolume);
Console.Write(" \n\n");
}
static void MolesCalculator()
{
Console.Clear();
Console.WriteLine("Moles Calaculator\n\n");
Console.WriteLine("Please enter mass of sample");
Decimal Mass = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Please Enter Your molar mass");
Decimal MolarMass = Convert.ToDecimal(Console.ReadLine());
Decimal Moles = Mass/MolarMass;
Console.Clear();
Console.WriteLine("Moles Calaculator\n\n");
Console.Write("Your sample has ");
Console.Write(Moles);
Console.Write(" moles\n\n");
}
static void EnergyOfWaveCalculator()
{
Console.Clear();
Console.WriteLine("Energy of Wave Calaculator\n\n");
Console.WriteLine("Please enter the frequency");
double Frequency = Convert.ToDouble(Console.ReadLine());
double PlancksConstant = 6.626e-34;
double Energy = PlancksConstant * Frequency;
Console.Clear();
Console.WriteLine("Energy of Wave Calaculator\n\n");
Console.Write("The answer is ");
Console.Write(Energy);
Console.Write(" \n\n");
}
static void IdealGasLawCalculator()
{
Console.Clear();
Console.WriteLine("Ideal Gas Law Calaculator\n\n");
Console.WriteLine("Would you like to solve the following equation for Pressure or Volume? Press v for Volume or p for Pressure");
string Frequency = Console.ReadLine();
if (Frequency == "v"){
Console.Clear();
Console.WriteLine("Ideal Gas Law Calaculator\n\n");
Console.WriteLine("Please enter the pressure");
decimal Pressure = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Please enter the the number of moles");
decimal NumberOfMoles = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Please enter the the temperature in degrees Kelvin");
decimal TemperatureKelvin = Convert.ToDecimal(Console.ReadLine());
decimal GasLawConstant = Convert.ToDecimal(8.314);
decimal IdealGasLaw = NumberOfMoles * GasLawConstant * TemperatureKelvin/Pressure;
Console.Clear();
Console.WriteLine("Energy of Wave Calaculator\n\n");
Console.Write("Your answer is ");
Console.Write(IdealGasLaw);
Console.Write(" \n\n");
}
else
{
Console.Clear();
Console.WriteLine("Ideal Gas Law Calaculator\n\n");
Console.WriteLine("Please enter the volume");
decimal Volume = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Please enter the the number of moles");
decimal NumberOfMoles = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Please enter the the temperature in degrees Kelvin");
decimal TemperatureKelvin = Convert.ToDecimal(Console.ReadLine());
decimal GasLawConstant = Convert.ToDecimal(8.314);
decimal IdealGasLaw = NumberOfMoles * GasLawConstant * TemperatureKelvin/Volume;
Console.Clear();
Console.WriteLine("Energy of Wave Calaculator\n\n");
Console.Write("Your answer is ");
Console.Write(IdealGasLaw);
Console.Write(" \n\n");
}
}
}
}
你在哪裏希望用戶可以選擇? –
這是一個很好的問題Mikael,我不確定在哪裏。我從他們最初的選擇中完成了他們的計算,我想要程序詢問他們是否想回到菜單 – Justin
好吧,我認爲最好的做法是爲計算菜單創建一個新方法,然後直接從主要方法,並在計算結束時重新調用該方法作爲選擇。檢查答案 –