C#简单传递参数问题

时间:2017-11-11 15:01:16

标签: c# parameter-passing pass-by-reference

我知道我现在听起来像个坏程序员 - 但我是新手,我无法弄清楚如何使用这个参考物并传递参数,我的意思是我知道该怎么做 - 但是同时 - 这不起作用,我不知道为什么。

static void Main(string[] args) {
  DealCard(ref card);
  Console.WriteLine();
  Console.ReadLine();
}

private static void DealCard(string card) {
  string finalNum = "";
  string finalSuite = "";
  bool diffCard = false;
  do {
    Random cardPicker = new Random();
    int cardSuite = cardPicker.Next(1, 5);
    if (cardSuite == 1) {
      finalSuite = "Hearts";
    } else if (cardSuite == 2) {
      finalSuite = "Spades";
    } else if (cardSuite == 3) {
      finalSuite = "Clubs";
    } else if (cardSuite == 4) {
      finalSuite = "Diamonds";
    }

    int cardNum = cardPicker.Next(1, 14);

    if (cardNum == 1) {
      finalNum = "Ace";
    } else if (cardNum == 2) {
      finalNum = "Two";
    } else if (cardNum == 3) {
      finalNum = "Thre";
    } else if (cardNum == 4) {
      finalNum = "Four";
    } else if (cardNum == 5) {
      finalNum = "Five";
    } else if (cardNum == 6) {
      finalNum = "Six";
    } else if (cardNum == 7) {
      finalNum = "Seven";
    } else if (cardNum == 8) {
      finalNum = "Eight";
    } else if (cardNum == 9) {
      finalNum = "Nine";
    } else if (cardNum == 10) {
      finalNum = "Ten";
    } else if (cardNum == 11) {
      finalNum = "Jack";
    } else if (cardNum == 12) {
      finalNum = "Queen";
    } else if (cardNum == 13) {
      finalNum = "King";
    }

    string newCard = finalNum + " of " + finalSuite;

    if (newCard != card) {
      card = finalNum + " of " + finalSuite;
      diffCard = true;
    } else {

    }
    card = newCard;

  } while (diffCard == false);
}

是的,我知道那么多,如果'是一个眼睛。 是的我知道我可以用不到一半的线来完成这个。 是的,我知道这是一个简单的问题。 是的,我知道我很糟糕,但是我想谦卑地要求任何人帮助我不要因此而失去头发。

2 个答案:

答案 0 :(得分:0)

您必须声明您的方法:

select(-WordID_count)

基本上该方法必须接受ref参数。

以下是支持答案的文档:

Value Type Parameters

Reference Type Parameters

答案 1 :(得分:0)

您的代码可以是这样的

public class Program
{

  public static void Main(string[] args) {
  string card = "";
  DealCard(ref card);

}

private static void DealCard(ref string card) 
{
  string finalNum = "";
  string finalSuite = "";
  bool diffCard = false;
  do {
      Random cardPicker = new Random();
      int cardSuite = cardPicker.Next(1, 5);
      string[] suite =  new String[]{"Hearts","Spades", "Clubs", "Diaminds"};


      int cardNum = cardPicker.Next(1, 3);
      string[] numbers = new String[]{"one","two","three", "four"};



    string newCard = numbers[cardNum] + " of " + suite[cardSuite];

    if (newCard != card) {
      card = finalNum + " of " + finalSuite;
      diffCard = true;
    } else {

    }
    card = newCard;
      Console.WriteLine(newCard);


  } while (diffCard == false);
  }
}