2016-11-17 52 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

你也可以看看爲機器中的物品創建一個類。一個類是一組相關的值和函數。您有一個項目名稱,項目數量,售出數量和機器中每個項目的價格。菜單可以是一個循環,通過您的項目類實例數組 –