C# 控制台计算器方法和菜单
我是c#新手,不知道如何在我的计算器应用程序中使用方法,我单独编写了这4个计算器,但不知道如何使用方法将其组合在一起。 我的导师说这个项目的每一个独立的行动/过程都应该是一种方法,我不知道如何去做 此计算器允许用户从以下主菜单中进行选择:C# 控制台计算器方法和菜单,c#,methods,console-application,calculator,C#,Methods,Console Application,Calculator,我是c#新手,不知道如何在我的计算器应用程序中使用方法,我单独编写了这4个计算器,但不知道如何使用方法将其组合在一起。 我的导师说这个项目的每一个独立的行动/过程都应该是一种方法,我不知道如何去做 此计算器允许用户从以下主菜单中进行选择: 长度计算器 体重指数计算器 腰围高度计算器 燃料消耗计算器 退出计算器 当前代码: static int ReadOption() { int option =0 ; bool ValidMainMenuOption = fa
static int ReadOption() {
int option =0 ;
bool ValidMainMenuOption = false;
do{
option = int.Parse(Console.ReadLine());
if ((1 <=option) & (option <= 5)) {
ValidMainMenuOption = true ;
} else {
ValidMainMenuOption = false;
} // end if
if (!ValidMainMenuOption){
Console.WriteLine("\n\t\a Option must be 1,2,3,4,5");
DisplayMenu();
} //end if
} while (!ValidMainMenuOption);
return option;
} //end ReadOption
/* Displays Main Menu
* Precondition:true
* postcondition: mainMenu displayed
*/
static void DisplayMenu() {
string mainMenu = "\n1)Length Calculator"
+ "\n2)Body Mass Index Calculator"
+ "\n3)Waist to Height Calculator"
+ "\n4)Fuel Consumption Calculator"
+ "\n5)Exit the Calculator"
+ "\n\n Enter your option(1,2,3,4 or 5 to exit):";
Console.Write(mainMenu);
} //end mainMenu
static void Main() {
const int Exit = 5;
int menuOption = ReadOption();
do {
if (menuOption != Exit) {
}// end if
} while (menuOption != Exit);
// need to output terminating message
Console.ReadKey();
} //end Main
} //end Class
// **LengthCalculator Code:**
static void Main(string[] args) {
double Centimetres = 0.0, Feet = 0.0, Inches = 0.0;
string AnotherConversion = null;
string LengthCalculatorMenu;
int LengthCalculatorOption;
do{
LengthCalculatorMenu = ("Enter 1) Convert centimetres to feet and inches:"
+ "\nEnter 2) Convert feet and inches to centimetres:");
Console.Write(LengthCalculatorMenu);
LengthCalculatorOption = int.Parse(Console.ReadLine());
if (LengthCalculatorOption == 1) {
Console.WriteLine("Please Enter the Centimetres(cm) that you wish to convert to feet and inches");
Centimetres = double.Parse(Console.ReadLine());
Feet = (Centimetres / 2.54) / 12;
int iFeet = (int)Feet;
Inches = (Feet - (double)iFeet) * 12;
Console.WriteLine("\nThe equivalent in feet and inches is {0:G1} ft {1:G1} ins", Feet, Inches);
Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any other key to exit):");
AnotherConversion = Console.ReadLine();
} else if (LengthCalculatorOption == 2) {
Console.WriteLine("Please Enter the Feet");
Feet = double.Parse(Console.ReadLine());
Console.WriteLine("Please Enter the Inches");
Inches = double.Parse(Console.ReadLine());
Centimetres = ((Feet * 12) + Inches) * 2.54;
Console.WriteLine("\nThe equivalent in centimetres is {0:G}cm", Centimetres);
Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any other key to exit):");
AnotherConversion = Console.ReadLine();
}
} while (AnotherConversion == "y" || AnotherConversion == "Y");
// **BMI Calculator code:**
static void Main(string[] args) {
double WeightKg = 0.0, HeightCm = 0.0, Weightlbs = 0.0, WeightOz = 0.0, BMI = 0.0, Feet = 0.0, Inches = 0.0;
int BMIOption;
string again = null;
do{
string BMIMenu = ("Which Measurement You Want to use to enter the weight and height?"
+ "\n1)Enter 1 for Metric"
+ "\n2)Enter 2 for British Imperial:");
Console.Write(BMIMenu);
BMIOption = int.Parse(Console.ReadLine());
if (BMIOption == 1) {
Console.Write("\nPlease Enter your Weight in Kilogram (kg):");
WeightKg = double.Parse(Console.ReadLine());
Console.Write("\nPlease Enter your Height in in centimetres (cm):");
HeightCm = double.Parse(Console.ReadLine());
BMI = WeightKg / (HeightCm / 100 * HeightCm / 100);
if (BMI >= 35.0) {
Console.WriteLine("\nYour BMI is {0:G},Severe Obesity", BMI);
} else if (BMI >= 30.0) {
Console.WriteLine("\nYour BMI is {0:G},Obese", BMI);
} else if (BMI >= 25.0) {
Console.WriteLine("\nYour BMI is {0:G},OverWeight", BMI);
} else if (BMI >= 18.5) {
Console.WriteLine("\nYour BMI is {0:G},Healthy BodyWeight", BMI);
} else if (BMI <= 18.5) {
Console.WriteLine("\nYour BMI is {0:G},UnderWeight", BMI);
}//End if
Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any other key to exit):");
again = Console.ReadLine();
} else if (BMIOption == 2) {
Console.WriteLine("Please Enter your Weight in Pounds (lbs):");
Weightlbs = double.Parse(Console.ReadLine());
Console.WriteLine("Please Enter your Weight in Ounces (oz):");
WeightOz = double.Parse(Console.ReadLine());
Console.WriteLine("Please Enter your Height in Feet (ft):");
Feet = double.Parse(Console.ReadLine());
Console.WriteLine("Please Enter your Height in Inches (ins):");
Inches = double.Parse(Console.ReadLine());
WeightKg = ((Weightlbs * 16) + WeightOz) / 35.2;
HeightCm = ((Feet * 12) + Inches) * 2.54;
BMI = WeightKg / (HeightCm / 100 * HeightCm / 100);
if (BMI >= 35) {
Console.WriteLine("Your BMI is {0:G},Severe Obesity", BMI);
} else if (BMI >= 30) {
Console.WriteLine("Your BMI is {0:G},Obese", BMI);
} else if (BMI >= 25) {
Console.WriteLine("Your BMI is {0:G},OverWeight", BMI);
} else if (BMI >= 18.5) {
Console.WriteLine("Your BMI is {0:G},Healthy BodyWeight", BMI);
} else if (BMI <= 18.5) {
Console.WriteLine("Your BMI is {0:G},UnderWeight", BMI);
}//End if
Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any other key to exit):");
again = Console.ReadLine();
}
} while (again == "y" || again == "Y");
// Waist to Height Code:
static void Main(string[] args) {
int WaistToHeightCalculatorOption;
int GenderOption;
string AnotherConversion = null;
double HeightCm = 0.0, WaistCm = 0.0;
double WaistToHeightRatio = 0.0;
double WaistIns = 0.0, HeightFeet = 0.0, HeightIns = 0.0, HeightTotalInIns = 0.0;
do {
string WaistToHeightCalculatorMenu = ("\nWhich Measurement You Want to use to enter the weight and height?"
+ "\n1)Enter 1 for Metric"
+ "\n2)Enter 2 for British Imperial:");
Console.Write(WaistToHeightCalculatorMenu);
WaistToHeightCalculatorOption = int.Parse(Console.ReadLine());
if (WaistToHeightCalculatorOption == 1) {
Console.Write("\nPlease Enter your Height in cm:");
HeightCm = double.Parse(Console.ReadLine());
Console.Write("\nPlease Enter your Waist in centimetres (cm):");
WaistCm = double.Parse(Console.ReadLine());
WaistToHeightRatio = WaistCm / HeightCm;
Console.Write("\n1)Enter 1 If you are Male"
+ "\n2)Enter 2 If you are Female:");
GenderOption = int.Parse(Console.ReadLine());
if (GenderOption == 1 && WaistToHeightRatio >= 0.536) {
Console.Write("\nYour Waist to Height Ration is {0}, Your Risk of Obesity Related Cardiovascular Diseases is at High Risk", WaistToHeightRatio);
} else if (GenderOption == 1 && WaistToHeightRatio < 0.536) {
Console.Write("\nYour Waist to Height Ration is {0}, Your Risk of Obesity Related Cardiovascular Diseases is at low Risk", WaistToHeightRatio);
} else if (GenderOption == 2 && WaistToHeightRatio >= 0.492) {
Console.Write("\nYour Waist to Height Ration is {0}, Your Risk of Obesity Related Cardiovascular Diseases is at High Risk", WaistToHeightRatio);
} else if (GenderOption == 2 && WaistToHeightRatio < 0.492) {
Console.Write("\nYour Waist to Height Ration is {0}, Your Risk of Obesity Related Cardiovascular Diseases is at low Risk", WaistToHeightRatio);
} //End if
Console.Write("\n\nWhould you like to make an anothe conversion? /n/n Enter Y to make an another conversion/Ener any other key to exit:");
AnotherConversion = Console.ReadLine();
} else if (WaistToHeightCalculatorOption == 2) {
Console.Write("\nPlase Enter your Waist in inches:");
WaistIns = double.Parse(Console.ReadLine());
Console.Write("\nPlease Enter the Height in feet:");
HeightFeet = double.Parse(Console.ReadLine());
Console.Write("\nPlease Enter the Heigt in inches:");
HeightIns = double.Parse(Console.ReadLine());
WaistToHeightRatio = WaistIns / HeightTotalInIns;
HeightTotalInIns = (HeightFeet * 12) + HeightIns;
Console.Write("\nMale Enter 1 , Female Enter 2:");
GenderOption = int.Parse(Console.ReadLine());
if (GenderOption == 1 && WaistToHeightRatio >= 0.536) {
Console.Write("Your Waist to Height Ration is {0}, Your Risk of Obesity Related Cardiovascular Diseases is at High Risk", WaistToHeightRatio);
} else if (GenderOption == 1 && WaistToHeightRatio < 0.536) {
Console.Write("Your Waist to Height Ration is {0}, Your Risk of Obesity Related Cardiovascular Diseases is at low Risk", WaistToHeightRatio);
} else if (GenderOption == 2 && WaistToHeightRatio >= 0.492) {
Console.Write("Your Waist to Height Ration is {0}, Your Risk of Obesity Related Cardiovascular Diseases is at High Risk", WaistToHeightRatio);
} else if (GenderOption == 2 && WaistToHeightRatio < 0.492) {
Console.Write("Your Waist to Height Ration is {0}, Your Risk of Obesity Related Cardiovascular Diseases is at low Risk", WaistToHeightRatio);
} //End if
Console.Write("\n\nWhould you like to make an anothe conversion? /n/n Enter Y to make an another conversion/Ener any other key to exit:");
AnotherConversion = Console.ReadLine();
}
} while (AnotherConversion == "Y" || AnotherConversion == "y");
// **Fuel Consumption code:**
static void Main(string[] args) {
int FuelConsumptionMenuOption;
double Litre = 0.0, Kilometre = 0.0, Gallon = 0.0 , Mile = 0.0 ;
double ComsumptionPer100Km = 0.0, ComsumptionPer100KmInMPG = 0.0,ComsumptionPer100KmInKm = 0.0, ComsumptionPerGal = 0.0;
ComsumptionPer100KmInMPG = ((Kilometre/1.609)/(Litre/4.546))*100;
string ToSeeMPerGalOption, ToSeelPerKmOption;
string AnotherConversion;
do{
string FuelConsumptionMenu = ("\nWhich Measurement You Want to use to enter the weight and height?"
+ "\n1)Enter 1 for Metric"
+ "\n2)Enter 2 for British Imperial:");
Console.Write(FuelConsumptionMenu);
FuelConsumptionMenuOption = int.Parse(Console.ReadLine());
if (FuelConsumptionMenuOption == 1) {
Console.Write("\nPlease Enter Litres(l) of Fuel used over distance travelled:");
Litre = double.Parse(Console.ReadLine());
Console.Write("\nPlease Enter Kilometres driven:");
Kilometre = double.Parse(Console.ReadLine());
ComsumptionPer100Km = Litre / (Kilometre / 100);
Console.WriteLine("\nYour Consumption in Litres per 100 Kilometres is {0}", ComsumptionPer100Km);
Console.Write("\nWould you like to see the equivalent result in miles per gallon (mpg)?"
+ "\n(Press Y For yes or Press other key to cancel this option):");
ToSeeMPerGalOption = Console.ReadLine();
ComsumptionPer100KmInMPG = ((Kilometre / 1.609) / (Litre / 4.546)*100) ;
if (ToSeeMPerGalOption == "Y" || ToSeeMPerGalOption == "y") {
Console.Write("\nThe equivalent result in miles per gallon (mpg) is {0}", ComsumptionPer100KmInMPG);
}
Console.Write("\n\nWhould you like to make an anothe conversion? /n/n Enter Y to make an another conversion/Ener any other key to exit:");
AnotherConversion = Console.ReadLine();
} else if (FuelConsumptionMenuOption == 2) {
Console.Write("\nPlease Enter Gallons (gal) of Fuel used over distance travelled:");
Gallon = double.Parse(Console.ReadLine());
Console.Write("\nPlease Enter Miles (m)driven:");
Mile = double.Parse(Console.ReadLine());
ComsumptionPerGal = Gallon / Mile;
Console.WriteLine("\nYour Consumption in Miles per Gallon is {0}", ComsumptionPerGal);
Console.Write("\nWould you like to see the equivalent result in litres per 100 kilometres(km)?"
+ "\n(Press Y For yes or Press other key to cancel this option):");
ToSeelPerKmOption = Console.ReadLine();
ComsumptionPer100KmInKm = ((Mile * 1.609) / (Gallon * 4.546)) * 100;
if (ToSeelPerKmOption == "Y" || ToSeelPerKmOption == "y") {
Console.Write("\nThe equivalent result in litres per 100 kilometres(km) is {0}", ComsumptionPer100KmInKm);
}
}//End if
Console.Write("\n\nWhould you like to make an anothe conversion? /n/n Enter Y to make an another conversion/Ener any other key to exit:");
AnotherConversion = Console.ReadLine();
} while (AnotherConversion == "Y" || AnotherConversion == "y");
static int ReadOption(){
int选项=0;
bool ValidMainMenuOption=false;
做{
option=int.Parse(Console.ReadLine());
如果((1=30.0){
WriteLine(“\n您的BMI为{0:G},肥胖”,BMI);
}否则,如果(体重指数>=25.0){
WriteLine(“\n您的体重指数为{0:G},超重”,体重指数);
}否则,如果(体重指数>=18.5){
控制台.WriteLine(\n您的BMI为{0:G},健康体重),BMI);
}否则,如果(体重指数=35){
WriteLine(“你的体重指数是{0:G},严重肥胖”,BMI);
}否则,如果(体重指数>=30){
WriteLine(“你的BMI是{0:G},肥胖”,BMI);
}否则,如果(体重指数>=25){
WriteLine(“你的体重指数是{0:G},超重”,体重指数);
}否则,如果(体重指数>=18.5){
WriteLine(“你的BMI是{0:G},健康体重”,BMI);
}否则如果(体重指数=0.536){
控制台。写(“\n你的腰围与身高之比为{0},你患肥胖相关心血管疾病的风险很高”,腰围与身高之比);
}否则,如果(性别选项==1和腰高比<0.536){
控制台。写下(“\n你的腰围与身高之比为{0},你患肥胖相关心血管疾病的风险较低”,腰围与身高之比);
}否则如果(性别选项==2和腰高比>=0.492){
控制台。写(“\n你的腰围与身高之比为{0},你患肥胖相关心血管疾病的风险很高”,腰围与身高之比);
}否则,如果(性别选项==2&&腰高比<0.492){
控制台。写下(“\n你的腰围与身高之比为{0},你患肥胖相关心血管疾病的风险较低”,腰围与身高之比);
}//如果结束,则结束
Console.Write(“\n\n是否要在转换过程中进行另一次转换?/n/n输入Y进行另一次转换/Ener任何其他要退出的键:”);
AnotherConversion=Console.ReadLine();
}否则如果(腰围高度计算器选项==2){
控制台。写(“\n请以英寸为单位输入您的腰围:”;
weighins=double.Parse(Console.ReadLine());
控制台。写(“\n请输入以英尺为单位的高度:”;
HeightFeet=double.Parse(Console.ReadLine());
控制台。写入(“\n请输入以英寸为单位的高度:”;
HeightIns=double.Parse(Console.ReadLine());
腰围高度比=腰围/高度总和;
高度总和=(高度英尺*12)+高度英寸;
控制台。写入(“\n可以输入1,可以输入2:”;
GenderOption=int.Parse(Console.ReadLine());
如果(性别选项==1和腰高比>=0.536){
控制台。写下(“你的腰高比为{0},你患肥胖相关心血管疾病的风险很高”,腰高比);
}否则,如果(性别选项==1和腰高比<0.536){
控制台。写下(“你的腰高比为{0},你患肥胖相关心血管疾病的风险较低”,腰高比);
}否则如果(性别选项==2和腰高比>=0.492){
控制台。写下(“你的腰高比为{0},你患肥胖相关心血管疾病的风险很高”,腰高比);
}否则,如果(性别选项==2&&腰高比<0.492){
控制台。写下(“你的腰高比为{0},你患肥胖相关心血管疾病的风险较低”,腰高比);
}//如果结束,则结束
Console.Write(“\n\n是否要在转换过程中进行另一次转换?/n/n输入Y进行另一次转换/Ener任何其他要退出的键:”);
AnotherConversion=Console.ReadLine();
}
}而(另一个转换==“Y”|另一个转换==“Y”);
//**燃油消耗代码:**
静态void Main(字符串[]参数){
int FuelConsumption菜单选项;
双升=0.0,公里=0.0,加仑=0.0,英里=0.0;
每100公里双消耗=0.0,每100公里消耗=0.0,每100公里消耗=0.0,每100公里消耗=0.0,每加仑消耗=0.0;
每100公里每加仑的消费量=((公里/1.609)/(升/4.546))*100;
字符串到seempergraption,到seelperkmoption;
字符串转换;
做{
字符串FuelConsumptionMenu=(“\n要使用哪个测量值来输入体重和身高?”
+“\n1)为度量输入1”
+“\n2)输入2表示不列颠帝国:”;
控制台。写入(FuelConsumption菜单);
FuelConsumptionMenuOption=int.Parse(Console.ReadLine());
如果(燃油消耗量菜单选项==1){
控制台。写(“\n请输入行驶距离内使用的燃油升:”;
升=double.Parse(Console.ReadLine());
控制台。写入(“\n请输入:”);
km=double.Parse(Console.ReadLine());
每100公里消费=升/(公里/100);
Console.WriteLine(“\n您的消耗量,单位为升/100千克
static void Main(string[] args) {
static void LengthCalculator(string[] args) {
static void Main() {
const int Exit = 5;
int menuOption = ReadOption();
do {
if (menuOption != Exit) {
// do something
}
} while (menuOption != Exit);
// need to output terminating message
Console.ReadKey();
}
if (menuOption == 1) {
LengthCalculator();
} else if (menuOption == 2) {
BodyMassIndexCalculator();
}
internal interface ICalculator
{
double Calculate(params double[] values);
}
internal class BmiCalculator : ICalculator
{
/// <summary>
/// </summary>
/// <param name="values">Mass, height</param>
/// <returns></returns>
public double Calculate(params double[] values)
{
if (values == null) throw new ArgumentNullException("values");
if (values.Length < 2) throw new ArgumentOutOfRangeException("values");
double mass = values[0];
double height = values[1];
return mass / (Math.Pow(height, 2.0d));
}
}
public class FuelConsumptionCalculator : ICalculator
{
/// <summary>
/// </summary>
/// <param name="values">Litres, per kilometers, distance</param>
/// <returns></returns>
public double Calculate(params double[] values)
{
if (values == null) throw new ArgumentNullException("values");
if (values.Length < 3) throw new ArgumentOutOfRangeException("values");
double liters = values[0];
double perKilometers = values[1];
double distance = values[2];
return (liters * perKilometers) / distance;
}
}
internal enum Calculation
{
Bmi,
FuelConsumption
}
internal class Demo
{
public Demo()
{
var valuesBmi = new[] { 60.0d, 1.70d };
var valuesFc = new[] { 65.0d, 100.0d, 500.0d };
double withEnum1 = WithEnum(Calculation.Bmi, valuesBmi);
double withEnum2 = WithEnum(Calculation.FuelConsumption, valuesFc);
double withGenerics1 = WithGenerics<BmiCalculator>(valuesBmi);
double withGenerics2 = WithGenerics<FuelConsumptionCalculator>(valuesFc);
double manually1 = new BmiCalculator().Calculate(valuesBmi);
double manually2 = new FuelConsumptionCalculator().Calculate(valuesFc);
}
private static double WithGenerics<T>(params double[] values) where T : ICalculator, new()
{
var foo = new T();
return foo.Calculate(values);
}
private static double WithEnum(Calculation calculation, double[] values)
{
double calculate;
switch (calculation)
{
case Calculation.Bmi:
var bmiCalculator = new BmiCalculator();
calculate = bmiCalculator.Calculate(values);
break;
case Calculation.FuelConsumption:
var fuelConsumptionCalculator = new FuelConsumptionCalculator();
calculate = fuelConsumptionCalculator.Calculate(values);
break;
default:
throw new ArgumentOutOfRangeException();
}
return calculate;
}
}