如何在控制台应用程序中实现简单的基于文本的菜单?

我想在折扣价格计算器中选择我为了启动程序而写的乐趣,并且想知道我是如何最佳地去做的。 我希望它能说出类似的话:“你想再输入另一个价格吗?” 如果用户说“是”或“是”或“否”等,请重启程序或退出。我应该使用if循环吗? 有人可以告诉我如何实现它吗? 还是指出我正确的方向? 我也觉得我应该重新编写程序以获得方法,但我不熟悉C#。

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Figure_the_Discount { class Program { static void Main(string[] args) { string price, discount; decimal discountedPrice, savedAmount; //Receiving the price as input Console.WriteLine("Please enter the price of the item."); price = Console.ReadLine(); decimal numPrice = decimal.Parse(price); //Receiving the discount as input Console.WriteLine("Please enter the discount that you wish to apply"); discount = Console.ReadLine(); //Receiving discount from input, divide by 100 to convert to percentile decimal numDiscount = decimal.Parse(discount) / 100; //Calculate the discounted price with price - (price * discount) discountedPrice = numPrice - (numPrice * numDiscount); //Calculate the amount of money they saved savedAmount = numPrice - discountedPrice; Console.WriteLine("The discounted price of this item is: ${0}\nYou saved: ${1}", discountedPrice, savedAmount); Console.ReadLine(); } } } 

只要用户在程序结束时指定yyes (不区分大小写),以下循环将循环执行。

 static void Main(string[] args) { string price, discount; decimal discountedPrice, savedAmount; bool startAgain = true; string line; // Loop again every time the startAgain flag is true. while (startAgain) { //Receiving the price as input Console.WriteLine("Please enter the price of the item."); price = Console.ReadLine(); decimal numPrice = decimal.Parse(price); //Receiving the discount as input Console.WriteLine("Please enter the discount that you wish to apply"); discount = Console.ReadLine(); //Receiving discount from input, divide by 100 to convert to percentile decimal numDiscount = decimal.Parse(discount) / 100; //Calculate the discounted price with price - (price * discount) discountedPrice = numPrice - (numPrice * numDiscount); //Calculate the amount of money they saved savedAmount = numPrice - discountedPrice; Console.WriteLine("The discounted price of this item is: ${0}\nYou saved: ${1}", discountedPrice, savedAmount); Console.ReadLine(); // Ask if the user wants to submit another price. Console.WriteLine("Would you like to enter another price?"); // Record the spaceless, lower-case answer. line = Console.ReadLine().ToLowerCase().Trim(); // Set the startAgain flag to true only if the line was "y" or "yes". startAgain = line == "y" || line == "yes"; } } 

只是粗略的样本

 string input = null; do { //your code Console.WriteLine("Would you like to insert another price?"); input = Console.ReadLine(); if(input.ToLower() != "y" || //if responce to the question is not 'y' or 'yes', break the loop input.ToLower() != "yes") break; }while(true); 

编辑

该程序插入价格,直到用户确认他想要插入新价格,否则只退出。

你可以这样做:

 static void Main(string[] args) { string resp = ""; string price, discount; decimal discountedPrice, savedAmount; do { .... // your previous code here .... Console.WriteLine("The discounted price of this item is: ${0}\nYou saved: ${1}", discountedPrice, savedAmount); Console.WriteLine("Another item?"); string resp = Console.ReadLine().ToLower(); } while (resp == "y" || resp == "yes"); Console.ReadLine(); }