2017-01-22 52 views
0

我已经编写了一个相当简单的程序,该程序显示选项菜单并根据用户输入执行计算。我想使程序运行,以便询问用户是否希望继续返回菜单或退出程序。我在想它可能需要某种循环,但我不确定如何实现它。如何让用户选择继续执行程序还是退出程序(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"); 
      } 

     } 
    } 
} 
+0

你在哪里希望用户可以选择? –

+0

这是一个很好的问题Mikael,我不确定在哪里。我从他们最初的选择中完成了他们的计算,我想要程序询问他们是否想回到菜单 – Justin

+0

好吧,我认为最好的做法是为计算菜单创建一个新方法,然后直接从主要方法,并在计算结束时重新调用该方法作为选择。检查答案 –

回答

2

这很简单。你有正确的想法。

bool shouldContinue = true; 
while(shouldContinue){ 
     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("Press 5 to exit"); 
     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; 
      case "5": 
      shouldContinue = false; 
       break; 
     } 

} 
+1

非常感谢! – Justin

0

您可以定义一个新的方法Menu

把菜单代码在那里,然后就呼吁它当你需要它。在Main()结束时,您可以使用一个简单的案例

Console.WriteLine("Type 'exit' to quit"); 
if(Console.ReadLine() == "exit") 
{ 

}else 
{ 
    Menu(); 
} 
2

您可以使用相同的逻辑运算的选择,把所有的代码中循环。

更改你的代码Main

char action = 'Y';  //create varible for user choice (continue or not) 
while (action == 'Y') // add loop 
{ 
    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; 
    } 

    //add these lines 
    Console.WriteLine("Do you want to continue!\n\n\n");   
    Console.WriteLine("Press Y to continue"); 
    Console.WriteLine("Press N to finish");  
    action = Console.ReadKey().KeyChar; 
} 

或者更好的选择是:

把所有代码Main一些方法内(即Start()),然后在Main

char action = 'Y'; 
while (action == 'Y') 
{ 
    Start(); 

    Console.WriteLine("Do you want to continue!\n\n\n");   
    Console.WriteLine("Press Y to continue"); 
    Console.WriteLine("Press N to finish");  
    action = Console.ReadKey().KeyChar; 
} 
+1

非常感谢! – Justin

0

像这样改变你的代码..主要方法中的代码太多了:

static void Main(string[] args) 
{ 
    ChooseCalculationMenu(); 
} 

public void ChooseCalculationMenu() 
{ 
    // Put the code from Main here instead 
} 

,并在每个计算方法的末尾:

​​