2016-11-17 60 views
-2

这是我的自动售货机。我现在需要能够在交换机末端重复菜单,以允许用户返回自动售货机或退出程序。选择选项后自动售货机重复菜单?

我会使用while循环还是使用if/else语句。我尝试在一个while循环中嵌套整个事物,但是它重复了购买选项。

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

     } 
    } 
} 

}

+0

我看起来不像你曾经给过用户退出/退出选项。可能添加第四个选项来退出/退出,或询问用户是否想在第一次通过后购买更多...如果他们确实显示菜单,并且如果不退出/退出。 – JohnG

+0

正常情况下,自动售货机坐在您的选择选项,直到下一个用户出现。在这种情况下,我不确定退出功能是否合适。 –

回答

0

我猜你是很新的节目。

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

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

然后你可以看一下检查用户的输入是一个有效的数字之类的东西作为其中的一部分功能

+0

你也可以看看为机器中的物品创建一个类。一个类是一组相关的值和函数。您有一个项目名称,项目数量,售出数量和机器中每个项目的价格。菜单可以是一个循环,通过您的项目类实例数组 –