C# 如何从静态void方法返回变量

C# 如何从静态void方法返回变量,c#,return,scope,void,C#,Return,Scope,Void,我不熟悉C#和强类型语言。我正在为大学做一项作业,我的课程现在按计划进行。但是,我更改了2个静态void方法标题,使返回类型没有意识到这样做将导致扣分 Current method headings static bool DispenseCash(double amount, int whichAccount, bool validAmount) and static double WithdrawAmount(int whichAccount) must remain What they

我不熟悉C#和强类型语言。我正在为大学做一项作业,我的课程现在按计划进行。但是,我更改了2个静态void方法标题,使返回类型没有意识到这样做将导致扣分

Current method headings
static bool DispenseCash(double amount, int whichAccount, bool validAmount) and
static double WithdrawAmount(int whichAccount) must remain 

What they need to be.
static void DispenseCash(double amount) and
static void WithdrawAmount(int whichAccount)
我更改了它们,因为我不知道如何从
静态作废取款金额(int whichAccount) 并将其用作中的参数 静态现金(双倍金额)

我被迫使用两种方法,而不是使用一种更大的方法来解决问题

下面是我的代码片段,可以更好地解释这一切。为了简短起见,我只加入了相关部分

int whichAccount = int.Parse(Console.ReadLine());
do
{
double amount = WithdrawAmount(whichAccount);
validAmount = DispenseCash(amount, whichAccount, validAmount);
} while (validAmount == false);

//end of relevant method calls in main

static double WithdrawAmount(int whichAccount)    
{
Console.Write("\nPlease enter how much you would like to withdraw: $");
double amount = double.Parse(Console.ReadLine());       
return amount; 
}
//end WithdrawAmount
在下面的DispenceCash方法中,如果它是静态void DispenceCash(双倍金额),我如何将int whichAccount和bool validAmount传递给它并从中返回bool validAmount

private static bool DispenseCash(double amount, int whichAccount, bool validAmount)
{
int numOf20s;
int numOf50s;
double balenceMinusAmount = (accountBalances[whichAccount]) - Convert.ToInt32(amount); 

if((Convert.ToInt32(amount) >= 1) && (Convert.ToInt32(amount) % 50 == 0) &&   (balenceMinusAmount >= accountLimits[whichAccount]))
{

numOf50s = Convert.ToInt32(amount) / 50;
numOf20s = (Convert.ToInt32(amount) % 50) / 20;


Console.WriteLine("Number of 50's = {0}", numOf50s);
Console.WriteLine("Number of 20's = {0}", numOf20s);
accountBalances[whichAccount] = (accountBalances[whichAccount]) - amount;
return validAmount = true;
}

else
      {
          Console.WriteLine("Invalid entry");
          return validAmount = false;
      }
}

请记住,我根本无法更改方法标题。但是我可以调用其中的一个方法,或者在方法内部调用新方法。我尝试了一些不同的方法,但所有的尝试都失败了。

正如jdpenix所提到的,我不知道为什么会要求您这样做。它违背了基本的编程原则。也许我们不了解当前问题的全部背景

我能想到的唯一方法是在应用程序中使用静态变量

private static double withdrawalAmount;
private static int selectedAccount;
private static bool isValidAmount;
然后在您需要的方法中使用这些方法,例如:

public static void WithdrawAmount(int whichAccount)
    {
        Console.Write("\nPlease enter how much you would like to withdraw: $");
        withdrawalAmount = double.Parse(Console.ReadLine());
    }

只是一个旁注——如果你有一个方法、函数(不管它在你选择的语言中被调用),它有一些有意义的输出,它不应该是空的。听起来你的教授是个白痴。你可以使用c#中的out参数,这些参数可以在一个方法中更新:public void DoSomething(int arg1,int arg2,out int result)它们被要求不更改方法签名。它不包括out参数。谢谢你的回复,嗯,是的,out参数听起来更容易接受。但是没有其他方法可以在不更改方法签名的情况下将值放回main吗?+1是可行的解决方案,但eww gross。。。我试着假装对老师们在编程任务中提出的要求感到惊讶,他们完全同意——这是一种可怕的“被教”的方式(你甚至可以称之为“教学”?)。最好是在静态环境中学习函数式编程。谢谢你的回答。我可能误解了这个问题,但是的,我确实让它比我更复杂。