C# 选择选项后自动售货机是否重复菜单?
这是我的自动售货机。我现在需要能够重复开关末端的菜单,以允许用户返回自动售货机或退出程序 我会使用while循环还是if/else语句。我试着将整个东西嵌套在一个while循环中,但它重复了购买选项C# 选择选项后自动售货机是否重复菜单?,c#,C#,这是我的自动售货机。我现在需要能够重复开关末端的菜单,以允许用户返回自动售货机或退出程序 我会使用while循环还是if/else语句。我试着将整个东西嵌套在一个while循环中,但它重复了购买选项 int iNumCrisps = 10; int iCrispsBought; int iNumChocbars = 20; int iChocbarsBought; int iNumSweets = 30;
int iNumCrisps = 10;
int iCrispsBought;
int iNumChocbars = 20;
int iChocbarsBought;
int iNumSweets = 30;
int iSweetsBought;
double dTotalMoney = 0;
int iChoice;
{
//display the choices
Console.WriteLine("Vending Machine");
Console.WriteLine("1 - Buy chocbars");
Console.WriteLine("2 - Buy crisps");
Console.WriteLine("3 - Buy sweets");
// get the users choice
Console.Write("Enter your choice: ");
iChoice = Convert.ToInt32(Console.ReadLine());
//validate user input
while (iChoice < 1 || iChoice > 3)
{
Console.Write("Incorrect option. Please Re-Enter: ");
iChoice = Convert.ToInt32(Console.ReadLine());
}
switch (iChoice)
{
case 1: //user has chosen chocbars
Console.WriteLine();
Console.Write("How many chocbars do you wish to purchase?");
iChocbarsBought = Convert.ToInt32(Console.ReadLine());
iNumChocbars = iNumChocbars - iChocbarsBought;
dTotalMoney = dTotalMoney + (iChocbarsBought * 0.25);
Console.WriteLine("There are now" + iNumChocbars + " chocbars in the machine");
break;
case 2: //User has chosen crisps
Console.WriteLine();
Console.Write("How many crisps do you wish to purchase?");
iCrispsBought = Convert.ToInt32(Console.ReadLine());
iNumCrisps = iNumCrisps - iCrispsBought;
dTotalMoney = dTotalMoney + (iCrispsBought * 0.30);
Console.WriteLine("There are now" + iNumCrisps + " crisps in the machine");
break;
case 3: //user has chosen sweets
Console.WriteLine();
Console.Write("How many sweets do you wish to purchase?");
iSweetsBought = Convert.ToInt32(Console.ReadLine());
iNumSweets = iNumSweets - iSweetsBought;
dTotalMoney = dTotalMoney + (iSweetsBought * 0.20);
Console.WriteLine("There are now " + iNumSweets + " sweets in the machine");
break;
default:
Console.WriteLine("You must enter a number from 1 to 3");
break;
}// end switch
//validate user input
while (iChoice < 1 || iChoice > 3)
{
Console.Write("Incorrect option. Please Re-Enter: ");
iChoice = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("There is now" + dTotalMoney + "p in the machine");
Console.WriteLine();
Console.WriteLine("Press any key to close");
Console.WriteLine();
Console.ReadKey();
}
}
}
int iNumCrisps=10;
int ICrispsBull;
int iNumChocbars=20;
int Ichocbarsbound;
int iNumSweets=30;
int是买来的;
双dTotalMoney=0;
国际iChoice;
{
//显示选项
控制台。WriteLine(“自动售货机”);
Console.WriteLine(“1-购买巧克力”);
Console.WriteLine(“2-购买薯片”);
Console.WriteLine(“3-购买糖果”);
//让用户选择
控制台。写(“输入您的选择:”;
iChoice=Convert.ToInt32(Console.ReadLine());
//验证用户输入
而(iChoice<1 | | iChoice>3)
{
控制台。写入(“选项不正确。请重新输入:”);
iChoice=Convert.ToInt32(Console.ReadLine());
}
开关(iChoice)
{
案例1://用户已选择chocbars
Console.WriteLine();
控制台。写下(“您希望购买多少巧克力?”);
Ichocbarsbound=Convert.ToInt32(Console.ReadLine());
iNumChocbars=iNumChocbars-ichocbarsbound;
dTotalMoney=dTotalMoney+(Ichocbarsbound*0.25);
Console.WriteLine(“现在机器中有“+iNumChocbars+”止动块”);
打破
案例2://用户选择了薯片
Console.WriteLine();
控制台。写下(“你想买多少薯片?”);
icrispsbull=Convert.ToInt32(Console.ReadLine());
iNumCrisps=iNumCrisps-icrispsbull;
dTotalMoney=dTotalMoney+(icrispsbunk*0.30);
Console.WriteLine(“机器中现在有“+iNumCrisps+”薯片”);
打破
案例3://用户选择了糖果
Console.WriteLine();
控制台。写下(“你想买多少糖果?”);
isweetsbound=Convert.ToInt32(Console.ReadLine());
iNumSweets=iNumSweets-IsWeetsBunded;
dTotalMoney=dTotalMoney+(iSweetsbunded*0.20);
Console.WriteLine(“机器中现在有“+iNumSweets+”糖果”);
打破
违约:
Console.WriteLine(“必须输入1到3之间的数字”);
打破
}//终端开关
//验证用户输入
而(iChoice<1 | | iChoice>3)
{
控制台。写入(“选项不正确。请重新输入:”);
iChoice=Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine(“机器中现在有“+dTotalMoney+”p”);
Console.WriteLine();
Console.WriteLine(“按任意键关闭”);
Console.WriteLine();
Console.ReadKey();
}
}
}
}我想你对编程相当陌生 您需要做的是将菜单放入函数中。这是一个代码块,您可以从代码中的其他地方调用它。我猜这段代码来自主函数
private static int ShowMenu ()
{
int iChoice = 0;
//display the choices
Console.WriteLine("Vending Machine");
Console.WriteLine("1 - Buy chocbars");
Console.WriteLine("2 - Buy crisps");
Console.WriteLine("3 - Buy sweets");
// get the users choice
Console.Write("Enter your choice: ");
iChoice = Convert.ToInt32(Console.ReadLine());
return iChoice;
}
然后,您将使用此行显示菜单并获得选择
iChoice = ShowMenu();
然后,您可以检查用户输入是否为有效数字,以及类似的内容,作为此函数的一部分您还可以查看为机器中的项目创建一个类。类是一组相关的值和函数。您拥有机器中每个项目的项目名称、项目数量、销售数量和价格。然后,菜单可以是一个循环,遍历item类实例数组。看起来,您从未给用户退出/退出的选项。可能会添加第四个退出/退出选项,或者询问用户是否希望在第一次通过后购买更多。。。如果有,则显示菜单,如果没有,则退出/退出。通常自动售货机位于“选择”选项,直到下一个用户出现。我不确定退出功能在这种情况下是否合适。