我正在制作一个自动售货机控制台应用程序,无法将payingAmount(输入的金额)传递给第二种方法来计算要提供的找零额。
此处提供代码:
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()
{
}
}
}
我是一个完整的业余爱好者,对这个基本问题很抱歉。
您将传递该变量作为参数,而不是直接调用该变量。将变量用作第二种方法Drinks
的参数。
您需要通过接受参数来制作功能性饮料,零食和惊喜。像这样的东西:
public void drinks(int paymentAmount) {
//some logic here
}
然后您在main内部这样调用该函数:
public static void Main() {
int paymentAmount;
paymentAmount = Convert.ToInt32(Console.ReadLine());
drinks(paymentAmount);
}
在c#中,当您希望方法2知道方法1知道的一些信息时,请向方法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"
}
要声明方法的参数,请在括号中添加类型和参数名称列表:
public static int MyMethod(int a, int b)
{
return a + b;
}
您可以通过传递匹配类型的列表来调用它。
Console.WriteLine(MyMethod(3, 5)); // print out 8