Skip to main content
 首页 » 编程设计

c#之C# 中的函数之如何将整数值放入函数中

2024年02月13日12wayfarer

基本上我正在尝试将一个整数值放入函数中,我正在为自动售货机制作一个简单的程序,我可以在其中选择“检查机器”来计算机器中薯片的数量,基本上我遇到的问题是,当我查看机器中实际有多少薯片时,它说数量为 0..我该如何解决这个问题,以便它允许我实际查看机器中当前有多少薯片?

到目前为止,这是我的代码:

static void Main(string[] args) 
{ 
   //Declare variables 
   int iOption = 0; //used to store users menu option 
   int iFillCrisps = 0; //used to store the amount of crisps the user wants to add to the machine 
   int iBuyCrisps = 0; //used to store the amount of crisps the user wants to buy 
   int iTotalNumCrisps = 0; //used to show the total number of crisps in the machine 
 
   //Menu 
   while (iOption != 4) //Program continously loops until user types "4" which is the exit key 
   { 
      GetMenuOption(iOption); 
      iOption = Convert.ToInt32(Console.ReadLine()); 
 
      //Process menu 
      if (iOption == 1) 
      { 
         FillCrisps(iFillCrisps, iTotalNumCrisps); 
      } 
 
      else if (iOption == 2) 
      { 
         BuyCrisps(iBuyCrisps, iTotalNumCrisps); 
      } 
 
      else if (iOption == 3) 
      { 
         InspectMachine(ref iTotalNumCrisps); 
      } 
 
      else if (iOption == 4) 
      { 
         Console.WriteLine("Exit"); 
      } 
   } 
} 
 
static int GetMenuOption(int piOption) 
{ 
   Console.WriteLine("Vending machine"); 
   Console.WriteLine(); 
   Console.WriteLine("Please choose an option"); 
   Console.WriteLine("1: Fill machine with crisps"); 
   Console.WriteLine("2: Buy Crisps"); 
   Console.WriteLine("3: Inspect Machine"); 
   Console.WriteLine("4: Exit"); 
 
   return piOption; 
} 
 
static int FillCrisps(int piFillCrisps, int piTotalNumCrisps) 
{ 
   Console.WriteLine("Fill machine with crisps"); 
   Console.WriteLine("How many crisps would you like to add to the machine?"); 
   piFillCrisps = Convert.ToInt32(Console.ReadLine()); 
   piTotalNumCrisps = piFillCrisps + piTotalNumCrisps; 
   Console.WriteLine("You are adding " + piFillCrisps + " packs of crisps to them machine"); 
   Console.WriteLine("There are now " + piTotalNumCrisps + " packs of crisps in the machine"); 
 
   return piTotalNumCrisps; 
} 
 
static int BuyCrisps(int piBuyCrisps, int piTotalNumCrisps) 
{ 
   Console.WriteLine("Buy Crisps"); 
   Console.WriteLine("How many crisps would you like to buy?"); 
   piBuyCrisps = Convert.ToInt32(Console.ReadLine()); 
   piTotalNumCrisps = piTotalNumCrisps - piBuyCrisps; 
   Console.WriteLine("You are buying " + piBuyCrisps + " crisps"); 
   Console.WriteLine("There are now " + piTotalNumCrisps + " packs of crisps in the machine"); 
 
   return piTotalNumCrisps; 
} 
 
static void InspectMachine(ref int piTotalNumCrisps) //Needs a way of retrieving the total value into it 
{ 
   Console.WriteLine("Inspect Machine"); 
   Console.WriteLine("There are currently " + piTotalNumCrisps + " crisps in the machine."); 
} 

请您参考如下方法:

嗯,你看,通常情况下,如果你是店主,你会喜欢先计算你需要给客户多少零钱,然后才把零钱给他。反之则不然。

所以,修改代码

    GetMenuOption(iOption); 
    iOption = Convert.ToInt32(Console.ReadLine()); 

到

    iOption = Convert.ToInt32(Console.ReadLine()); 
    GetMenuOption(iOption); 

再试一次。

此外,其他功能只是从外部获取参数。他们正在做某事,但他们没有给予任何返回。例如,这个函数:

static int FillCrisps(int piFillCrisps, int piTotalNumCrisps) 

尝试更改计数器,但由于它只从外部“读取”参数,因此无论它计算出什么,在外部变量中都是不可见的。尝试将其更改为

static int FillCrisps(ref int piFillCrisps, ref int piTotalNumCrisps) 

并调整其余代码。现在更改也将在外部变量中可见。

另一方面,这个函数:

static void InspectMachine(ref int piTotalNumCrisps) 
         //Needs a way of retrieving the total value into it 

仅读取和打印值。它不会改变它们。您根本不需要此处的 ref。将其改回:

static void InspectMachine(int piTotalNumCrisps) 

这样就没问题了(只要你把其他功能也改正!)

因此,您的函数应具有以下特征:

// functions that don't change the data 
static int GetMenuOption(int piOption) 
static void InspectMachine(int piTotalNumCrisps) 
 
// functions that DO change the data 
static int FillCrisps(ref int piFillCrisps, ref int piTotalNumCrisps) 
static int BuyCrisps(ref int piBuyCrisps, ref int piTotalNumCrisps)