[英]usage of global variable on C#
所以我不知道C#的术语。 我想做的是我有2个静态空隙
static void SelectProduct() {
double moneyamount = 0;
int selection = int.Parse(Console.ReadLine());
if (selection == 1) {
moneyamount = 1.50;
}
else {
Console.WriteLine("Wrong Selection");
}
Console.WriteLine("Your drink costs $" + moneyamount);
InsertCoin();
}
static void InsertCoin() {
Console.WriteLine("Balance of cost $" + moneyamount);
}
我的问题是我想在InsertCoin void
使用moneyamount
。 当我在下课后立即分配双倍的金额时,会出现错误。 我不能退还定金,因为它是静态的。 而且我必须使用static来回忆它。 那么在这种情况下我该怎么办?
也许也使您的资金量保持不变。
static double moneyamount = 0;
static void SelectProduct() {
moneyamount = 0;
int selection = int.Parse(Console.ReadLine());
if (selection == 1) {
moneyamount = 1.50;
}
else {
Console.WriteLine("Wrong Selection");
}
Console.WriteLine("Your drink costs $" + moneyamount);
InsertCoin();
}
static void InsertCoin() {
Console.WriteLine("Balance of cost $" + moneyamount);
}
你有两个选择
选项1:将 moneyAmount作为参数传递给InsertCoin(),即
static void InsertCoin(double moneyAmount)...
然后从SelectProduct调用为
InsertCoin(moneyAmount);
选项2:简单地将全局变量声明为静态变量,即
static double moneyAmount = 0;
static void SelectProduct()...
static void InsertCoin()...
您在这里有2个选择。 您可以将moneyamount定义为2种方法之外的全局变量,并使其对两种方法均可用,也可以像声明方法那样声明并将其传递给InsertCoin方法。
方案1:
static double moneyamount = 0;
static void SelectProduct()
{
int selection = int.Parse(Console.ReadLine());
if (selection == 1)
{
moneyamount = 1.50;
}
else
{
Console.WriteLine("Wrong Selection");
}
Console.WriteLine("Your drink costs $" + moneyamount);
}
static void InsertCoin()
{
Console.WriteLine("Balance of cost $" + moneyamount);
}
方案2:
static void SelectProduct()
{
double moneyamount = 0;
int selection = int.Parse(Console.ReadLine());
if (selection == 1)
{
moneyamount = 1.50;
}
else
{
Console.WriteLine("Wrong Selection");
}
Console.WriteLine("Your drink costs $" + moneyamount);
InsertCoin(moneyamount);
}
static void InsertCoin(double moneyamount)
{
Console.WriteLine("Balance of cost $" + moneyamount);
}
我希望这有帮助。 如果需要更多帮助,请喊。
您可以将参数传递给方法。 请谷歌的“ C#功能语法”
static void InsertCoin(double moneyAmount)
{
Console.WriteLine("Balance of cost $" + moneyamount);
}
在类中而不是在类方法中声明您的moneyamount
,并将其也设置为static
。 这样,同一类中的静态方法可以访问您的静态变量moneyamount
。
将方法声明为static
意味着该方法是一个类的一部分,并且可以直接从另一个类访问。 如果没有static
,则成为对象实例的一部分,并且可以通过对象进行访问。 因此,静态方法访问静态变量:
static string fooString;
static void FooMethod()
{
fooString = "foo"
}
您可以创建MoneyAmount
的静态属性
static double MoneyAmount
{get; set;}
static void SelectProduct()
{
MoneyAmount= 0;
int selection = int.Parse(Console.ReadLine());
if (selection == 1) {
MoneyAmount = 1.50;
}
else {
Console.WriteLine("Wrong Selection");
}
Console.WriteLine("Your drink costs $" + MoneyAmount);
InsertCoin();
}
static void InsertCoin()
{
Console.WriteLine("Balance of cost $" + MoneyAmount);
}
或者您可以为InsertCoin()
参数
static void SelectProduct()
{
double moneyamount = 0;
int selection = int.Parse(Console.ReadLine());
if (selection == 1) {
moneyamount = 1.50;
}
else {
Console.WriteLine("Wrong Selection");
}
Console.WriteLine("Your drink costs $" + moneyamount);
InsertCoin();
}
static void InsertCoin(double moneyamount)
{
Console.WriteLine("Balance of cost $" + moneyamount);
}
另一个注意事项;
我无法退还
moneyamount
因为它是静态的
你为什么不能那样做?
static double SelectProduct()
{
...
return moneyamount;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.