选择选项后自动售货机重复菜单

本文关键字:菜单 自动售货机 选项 选择 | 更新日期: 2023-09-27 18:18:18

这是我的自动售货机。我现在需要能够在切换结束时重复菜单,以允许用户回到自动售货机或退出程序。

我会使用while循环或if/else语句吗?我尝试在while循环中嵌套整个东西,但随后它重复了purchase选项。

        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();
        }
    }
}

}

选择选项后自动售货机重复菜单

我猜你对编程还是个新手。

你需要做的是把菜单放在一个函数中。这是一个代码块,您可以从代码的其他地方调用它。我猜这段代码来自main函数?

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();

你可以检查用户的输入是否是一个有效的数字,诸如此类的事情作为这个函数的一部分