尝试做一个菜单循环
trying to do a menu loop
我正在 visual studio 自学 C# 并尝试在我的代码中添加一个循环,这样它就像一个菜单,用户可以在其中 select 一个选项,但可以随时退出该选项无需停止并重新启动整个代码。我只是想不出把它放在哪里才能使它 运行 正确 - 其余代码像菜单一样工作,但只让你 select 一个选项和 运行 它之前的一次停止。
这是我目前所拥有的;
class 程序
{
私有静态字符串成员资格;
static void Main(string[] args)
{
Console.Title = "Gym Menu";
Console.WriteLine("Welcome, What would you like to do?");
Console.WriteLine("");
Console.WriteLine("[1] BMI Calculator");
Console.WriteLine("[2] Choose Membership");
Console.WriteLine("[3] Exit");
string input = Console.ReadLine();
if (input == "1")
{
bmicalculator();
}
else if (input == "2")
{
choosemembership();
}
else if (input == "3")
{
Console.WriteLine("goodbye");
Environment.Exit(0);
}
}
public static void mainmenu()
{
Console.Clear();
Console.ReadLine();
}
public static void bmicalculator()
{
Console.WriteLine("BMI Calculator");
Console.WriteLine();
Console.Write("Weight in kg: ");
int kg; kg = Convert.ToInt32(Console.ReadLine());
Console.Write("Height in cm: ");
int m; m = Convert.ToInt32(Console.ReadLine());
double BMI; BMI = kg / ((m / 100.0) * (m / 100.0));
Console.WriteLine("your BMI is.." + BMI);
if (BMI < 18.5) { Console.WriteLine("-> Underweight"); }
if (BMI >= 18.5 & BMI <= 25) { Console.WriteLine("-> Normal"); }
if (BMI >= 25 & BMI <= 30) { Console.WriteLine("-> Overweight"); }
if (BMI >= 30) { Console.WriteLine("-> Obese"); }
Console.WriteLine("Press any key to continue or enter return to go to the menu..");
Console.ReadKey();
Console.ReadLine();
}
public static void choosemembership()
{
Console.WriteLine("Please choose your membership from the options below");
Console.WriteLine(" Basic pw - Regular pw - Premium pw");
membership = Console.ReadLine();
switch (membership)
{
case "Basic":
{
Console.WriteLine("You have chosen the basic membership for per week");
break;
}
case "Regular":
{
Console.WriteLine("You have chosen the regular membership for per week");
break;
}
case "Premium":
{
Console.WriteLine("You have chosen the premium membership for per week");
break;
}
default:
{
Console.WriteLine("You have not selected a valid membership type");
break;
}
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
}
如果有人能提供帮助,我将不胜感激:)
立即在 main 函数内做一个 while 循环...
static void Main(string[] args)
{
while (true)
{
.... existing code as is
}
{
用这个,注意有一行代码我注释掉了
static void Main(string[] args)
{
while (true)
{
Console.Title = "Gym Menu";
Console.WriteLine("Welcome, What would you like to do?");
Console.WriteLine("");
Console.WriteLine("[1] BMI Calculator");
Console.WriteLine("[2] Choose Membership");
Console.WriteLine("[3] Exit");
string input = Console.ReadLine();
if (input == "1")
{
bmicalculator();
}
else if (input == "2")
{
choosemembership();
}
else if (input == "3")
{
Console.WriteLine("goodbye");
Environment.Exit(0);
}
};
}
public static void mainmenu()
{
Console.Clear();
Console.ReadLine();
}
public static void bmicalculator()
{
Console.WriteLine("BMI Calculator");
Console.WriteLine();
Console.Write("Weight in kg: ");
int kg; kg = Convert.ToInt32(Console.ReadLine());
Console.Write("Height in cm: ");
int m; m = Convert.ToInt32(Console.ReadLine());
double BMI; BMI = kg / ((m / 100.0) * (m / 100.0));
Console.WriteLine("your BMI is.." + BMI);
if (BMI < 18.5) { Console.WriteLine("-> Underweight"); }
if (BMI >= 18.5 & BMI <= 25) { Console.WriteLine("-> Normal"); }
if (BMI >= 25 & BMI <= 30) { Console.WriteLine("-> Overweight"); }
if (BMI >= 30) { Console.WriteLine("-> Obese"); }
Console.WriteLine("Press any key to continue or enter return to go to the menu..");
Console.ReadKey();
// Console.ReadLine();
}
public static void choosemembership()
{
Console.WriteLine("Please choose your membership from the options below");
Console.WriteLine(" Basic pw - Regular pw - Premium pw");
var membership = Console.ReadLine();
switch (membership)
{
case "Basic":
{
Console.WriteLine("You have chosen the basic membership for per week");
break;
}
case "Regular":
{
Console.WriteLine("You have chosen the regular membership for per week");
break;
}
case "Premium":
{
Console.WriteLine("You have chosen the premium membership for per week");
break;
}
default:
{
Console.WriteLine("You have not selected a valid membership type");
break;
}
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
我正在 visual studio 自学 C# 并尝试在我的代码中添加一个循环,这样它就像一个菜单,用户可以在其中 select 一个选项,但可以随时退出该选项无需停止并重新启动整个代码。我只是想不出把它放在哪里才能使它 运行 正确 - 其余代码像菜单一样工作,但只让你 select 一个选项和 运行 它之前的一次停止。
这是我目前所拥有的;
class 程序 { 私有静态字符串成员资格;
static void Main(string[] args)
{
Console.Title = "Gym Menu";
Console.WriteLine("Welcome, What would you like to do?");
Console.WriteLine("");
Console.WriteLine("[1] BMI Calculator");
Console.WriteLine("[2] Choose Membership");
Console.WriteLine("[3] Exit");
string input = Console.ReadLine();
if (input == "1")
{
bmicalculator();
}
else if (input == "2")
{
choosemembership();
}
else if (input == "3")
{
Console.WriteLine("goodbye");
Environment.Exit(0);
}
}
public static void mainmenu()
{
Console.Clear();
Console.ReadLine();
}
public static void bmicalculator()
{
Console.WriteLine("BMI Calculator");
Console.WriteLine();
Console.Write("Weight in kg: ");
int kg; kg = Convert.ToInt32(Console.ReadLine());
Console.Write("Height in cm: ");
int m; m = Convert.ToInt32(Console.ReadLine());
double BMI; BMI = kg / ((m / 100.0) * (m / 100.0));
Console.WriteLine("your BMI is.." + BMI);
if (BMI < 18.5) { Console.WriteLine("-> Underweight"); }
if (BMI >= 18.5 & BMI <= 25) { Console.WriteLine("-> Normal"); }
if (BMI >= 25 & BMI <= 30) { Console.WriteLine("-> Overweight"); }
if (BMI >= 30) { Console.WriteLine("-> Obese"); }
Console.WriteLine("Press any key to continue or enter return to go to the menu..");
Console.ReadKey();
Console.ReadLine();
}
public static void choosemembership()
{
Console.WriteLine("Please choose your membership from the options below");
Console.WriteLine(" Basic pw - Regular pw - Premium pw");
membership = Console.ReadLine();
switch (membership)
{
case "Basic":
{
Console.WriteLine("You have chosen the basic membership for per week");
break;
}
case "Regular":
{
Console.WriteLine("You have chosen the regular membership for per week");
break;
}
case "Premium":
{
Console.WriteLine("You have chosen the premium membership for per week");
break;
}
default:
{
Console.WriteLine("You have not selected a valid membership type");
break;
}
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
}
如果有人能提供帮助,我将不胜感激:)
立即在 main 函数内做一个 while 循环...
static void Main(string[] args)
{
while (true)
{
.... existing code as is
}
{
用这个,注意有一行代码我注释掉了
static void Main(string[] args)
{
while (true)
{
Console.Title = "Gym Menu";
Console.WriteLine("Welcome, What would you like to do?");
Console.WriteLine("");
Console.WriteLine("[1] BMI Calculator");
Console.WriteLine("[2] Choose Membership");
Console.WriteLine("[3] Exit");
string input = Console.ReadLine();
if (input == "1")
{
bmicalculator();
}
else if (input == "2")
{
choosemembership();
}
else if (input == "3")
{
Console.WriteLine("goodbye");
Environment.Exit(0);
}
};
}
public static void mainmenu()
{
Console.Clear();
Console.ReadLine();
}
public static void bmicalculator()
{
Console.WriteLine("BMI Calculator");
Console.WriteLine();
Console.Write("Weight in kg: ");
int kg; kg = Convert.ToInt32(Console.ReadLine());
Console.Write("Height in cm: ");
int m; m = Convert.ToInt32(Console.ReadLine());
double BMI; BMI = kg / ((m / 100.0) * (m / 100.0));
Console.WriteLine("your BMI is.." + BMI);
if (BMI < 18.5) { Console.WriteLine("-> Underweight"); }
if (BMI >= 18.5 & BMI <= 25) { Console.WriteLine("-> Normal"); }
if (BMI >= 25 & BMI <= 30) { Console.WriteLine("-> Overweight"); }
if (BMI >= 30) { Console.WriteLine("-> Obese"); }
Console.WriteLine("Press any key to continue or enter return to go to the menu..");
Console.ReadKey();
// Console.ReadLine();
}
public static void choosemembership()
{
Console.WriteLine("Please choose your membership from the options below");
Console.WriteLine(" Basic pw - Regular pw - Premium pw");
var membership = Console.ReadLine();
switch (membership)
{
case "Basic":
{
Console.WriteLine("You have chosen the basic membership for per week");
break;
}
case "Regular":
{
Console.WriteLine("You have chosen the regular membership for per week");
break;
}
case "Premium":
{
Console.WriteLine("You have chosen the premium membership for per week");
break;
}
default:
{
Console.WriteLine("You have not selected a valid membership type");
break;
}
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}