我如何在另一个方法中使用main()方法中定义的值

时间:2019-11-17 18:36:54

标签: c# parameter-passing

我正在制作一个自动售货机控制台应用程序,无法将paymentAmount(输入的金额)传递给第二种方法来计算要提供的找零额。

此处提供代码:

using System;

namespace Vending_Machine
{
    class Program
    {

        public static void Main()
        {
            //this is the variable i would like to use
            int itemClass, paymentAmount;
            Console.BackgroundColor = ConsoleColor.DarkBlue;
            Console.SetCursorPosition((Console.WindowWidth - 14) / 2, Console.CursorTop);
            Console.WriteLine("Welcome to Vending Machine V1");
            Console.BackgroundColor = ConsoleColor.Blue;
            Console.WriteLine("Please Insert your payment (value between 1 and 10)");
            paymentAmount = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("What would you like to buy");
            Console.ResetColor();
            Console.WriteLine("1.Drinks \n2.Snacks \n3.Surprise Me!");
            itemClass = Convert.ToInt32(Console.ReadLine());
            Console.Clear();
            switch (itemClass)
            {
                case 1:
                    Drinks();
                    break;
                case 2:
                    Snacks();
                    break;
                case 3:
                    Surprise();
                    break;
            }
        }
        public static void Drinks()
        {
            int drinkType, paymentChange;
            //this is where i would like to equate it to paymentChange
            Console.BackgroundColor = ConsoleColor.Blue;
            Console.WriteLine("Which Drink would You like to Buy");
            Console.ResetColor();
            Console.WriteLine("1.Pepsi\n2.Coca-Cola\n3.Diet Coke\n4.Coca-Cola with Zesty Blood Orange\n5.Dasani\n6.Fanta\n7.Dr.Pepper\n8.Moxie\n9.Cold Coffee\n10.Red Bull");
            drinkType = Convert.ToInt32(Console.ReadLine());
            Console.BackgroundColor = ConsoleColor.Green;
            switch (drinkType)
            {
                case 1:
                    Console.WriteLine("this will cost you $1\nPress any key to continue.");
                    //this is where i would subtract the cost of the drink from the total and present it as change
                    break;
                case 2:
                    Console.WriteLine("this will cost you $1\nPress any key to continue.");

                    break;
                case 3:
                    Console.WriteLine("this will cost you $1\nPress any key to continue.");

                    break;
                case 4:
                    Console.WriteLine("this will cost you $1\nPress any key to continue.");

                    break;
                case 5:
                    Console.WriteLine("this will cost you $1\nPress any key to continue.");

                    break;
                case 6:
                    Console.WriteLine("this will cost you $1\nPress any key to continue.");

                    break;
                case 7:
                    Console.WriteLine("this will cost you $1\nPress any key to continue.");

                    break;
                case 8:
                    Console.WriteLine("this will cost you $1\nPress any key to continue.");

                    break;
                case 9:
                    Console.WriteLine("this will cost you $1\nPress any key to continue.");

                    break;
                case 10:
                    Console.WriteLine("this will cost you $1\nPress any key to continue.");

                    break;
            }
        }
        public static void Snacks()
        {

        }
        public static void Surprise()
        {

        }
    }
}

我是个彻头彻尾的业余爱好者,很抱歉出现这个基本问题。

编辑:谢谢大家的帮助。现在正在工作。最简单的方法是将int作为参数传递。这是一次很好的学习经历。

5 个答案:

答案 0 :(得分:2)

您需要通过接受参数来制作功能饮料,零食和惊喜。像这样:

public void drinks(int paymentAmount) {
    //some logic here
}

然后您像下面这样在main内部调用该函数:

public static void Main() {
   int paymentAmount;
   paymentAmount = Convert.ToInt32(Console.ReadLine());
   drinks(paymentAmount);
}

答案 1 :(得分:2)

在c#中,当您希望方法2知道方法1知道的某些信息时,请在调用方法2时向方法2的签名添加参数,然后在方法1中为该参数提供一个值。

public void Method1(){ //this method has no parameters

  int x = 5;

  Method2(x); passes the value of x, to method 2
}

public void Method2(int someNumber){ //this method has one, integer, parameter

  Console.Out.WritLine(someNumber); //prints out "5", which originally came from the value of x

}

请务必注意,Method2无法访问Method1中的x。如果Method2更改了someNumber的值,则x的值不受影响。简单来说,我们将其称为“按值传递”-方法1中的x值传递给方法2,而不是实际变量。您可能会想到,方法2调用后,c#运行以下行:int someNumber = 5->它建立了另一个名为someNumber的变量,并从x的值中获得了值,但它们显然是不相关的-它们恰好具有相同的值

如果我们要传递更复杂的对象(例如较大的对象),则可以更改有关该对象的内容,Method1将看到更改,但是我们仍然无法将其换成一个新的对象,而让Method1看到更改,除非它与ref或out关键字一起传递。通常,我们将其称为“通过引用传递”,但这有点不恰当,因为事情始终是变量引用。不过,这些绝对是可以重来一次的概念(后来,当您有更多的编码经验并且完全掌握了一些基本概念时)

就目前而言,在传递诸如ints之类的简单内容时,要知道在Method2中更改值不会影响Method1中的值。如果Method1需要知道Method2将值更改为哪个值,则方法2应该将值返回给Method1:

public void Method1(){ //this method has no parameters

  int x = 5;

  int newX = Method2(x); passes the value of x, to method 2. newX will be set to 10 when Method2 finishes
}

public int Method2(int someNumber){ //this method has one, integer, parameter

  return someNumber + 5; //returns 10 back to the calling method, Method1

}

答案 2 :(得分:1)

要声明方法的参数,请在括号中添加类型和参数名称的列表:

public static int MyMethod(int a, int b)
{
  return a + b;
}

您可以通过传递匹配类型的列表来调用它。

Console.WriteLine(MyMethod(3, 5)); // print out 8

答案 3 :(得分:0)

您将传递该变量作为参数,而不是直接调用该变量。 将变量用作第二个方法Drinks的参数。

答案 4 :(得分:0)

您可以在类级别上初始化变量。

示例:

using System;

namespace Vending_Machine
{
    class Program
    {
        private static int itemClass;
        private static int paymentAmount;
        private static int drinkType;
        private static int paymentChange;

        public static void Main() {...}

        public static void Drinks() {...}

        public static void Snacks(){...}

        public static void Surprise(){...}
    }
}

这样,您将能够在Vending_Machine的方法中使用变量。