如何在控制台应用程序中实现一个简单的基于文本的菜单?我想选择折扣价格计算器来享受我为启动该程序而编写的乐趣,并且想知道我如何最好地去做。我想让它说这样的话:“你想输入另一个价格吗?”如果用户说“是”或“是”或“否”等,则重新启动程序或退出。我应该使用if循环吗?有人能告诉我如何实现吗?或者指出我正确的方向?我也觉得我应该重新编写程序以获得这些方法,但我不熟悉C#。使用系统;使用System.Collections.Generic;使用System.Linq;使用系统文本;namespaceFigure_the_Discount{classProgram{staticvoidMain(string[]args){字符串价格,折扣;十进制折扣价格,saveAmount;//接收价格作为输入Console.WriteLine("请输入商品的价格。");价格=Console.ReadLine();十进制numPrice=decimal.Parse(price);//接收折扣作为输入Console.WriteLine("Pleaseenterthediscountthatyouwishtoapply");折扣=Console.ReadLine();//从输入接收折扣,除以100转换为百分位小数numDiscount=decimal.Parse(discount)/100;//计算折扣价price-(price*discount)discountedPrice=numPrice-(numPrice*numDiscount);//计算他们存了多少钱saveAmount=numPrice-discountedPrice;Console.WriteLine("该商品的折扣价为:${0}n您节省了:${1}",discountedPrice,savedAmo单位);控制台.ReadLine();只要用户在程序末尾指定y或yes(不区分大小写),下面的循环就会循环staticvoidMain(string[]args){字符串价格,折扣;十进制折扣价格,saveAmount;布尔再次启动=真;弦线;//每次startAgain标志为真时再次循环。while(startAgain){//接收价格作为输入Console.WriteLine("请输入商品的价格。");价格=Console.ReadLine();十进制numPrice=decimal.Parse(price);//接收折扣作为输入Console.WriteLine("Pleaseenterthediscountthatyouwishtoapply");折扣=Console.ReadLine();//从输入接收折扣,除以100转换为百分位小数numDiscount=decimal.Parse(discount)/100;//计算折扣价price-(price*discount)discountedPrice=numPrice-(numPrice*numDiscount);//计算他们存了多少钱saveAmount=numPrice-discountedPrice;Console.WriteLine("该商品的折扣价为:${0}n您节省了:${1}",discountedPrice,savedAmount);控制台.ReadLine();//询问用户是否要提交另一个价格。Console.WriteLine("你想输入另一个价格吗?");//记录无空格的小写答案。line=Console.ReadLine().ToLowerCase().Trim();//仅当该行为“y”或“yes”时才将startAgain标志设置为true。startAgain=line=="y"||行==“是”;}}粗略样本字符串input=null;do{//你的代码Console.WriteLine("Wouldyouliketoinsertanotherprice?");输入=Console.ReadLine();if(input.ToLower()!="y"||//如果对问题的回答不是'y'或'yes',则中断循环input.ToLower()!="yes")break;}而(真);编辑程序插入价格,直到用户确认Hewantstoinsertthenewprice,否则直接退出你可以这样做:以上是C#学习教程:Howtoimplementasimpletext-basedmenuinaconsoleapplication?如果分享的内容对你有用,需要了解更多C#学习教程,希望你多多关注——staticvoidMain(string[]args){stringresp="";字符串价格,折扣;十进制折扣价格,saveAmount;do{....//你之前的代码在这里....Console.WriteLine("这件商品的折扣价是:${0}n你节省了:${1}",discountedPrice,savedAmount);Console.WriteLine("另一个项目?");stringresp=Console.ReadLine().ToLower();}while(resp=="y"||resp=="yes");控制台.ReadLine();}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
