C# 中的函数 - 如何将整数值传入函数?

问题描述 投票:0回答:3

基本上我正在尝试将整数值放入函数中,我正在为自动售货机制作一个简单的程序,其中我可以选择“检查机器”来计算机器中薯片的数量,基本上是问题我的问题是,当我查看机器中实际有多少薯片时,它说数量为 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.");
}
c# function integer
3个回答
2
投票

嗯,你看,通常,如果你是店主,你会先计算一下你需要还给顾客多少零钱,然后才给他零钱。不是相反。

所以,更改代码

    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)

2
投票

有一种比上面提到的方法更简单的方法,您只需在即将发生的实际函数之前添加“iTotalNumCrisps =”..例如:

            if (iOption == 1)
              {
                iTotalNumCrisps = FillCrisps(iFillCrisps, iTotalNumCrisps);
              }

0
投票

要使 ReadLine() 函数返回整数,请使用以下命令:

System.Int32.Parse(Console.ReadLine());

© www.soinside.com 2019 - 2024. All rights reserved.