C# “无过载方法”;击中;接受1个参数。需要返回两个列表
在main中调用类函数时遇到问题,因为它需要同时返回两个列表。该函数将一张卡片从一个列表添加到另一个列表,然后将其从原始列表中删除。但是当我尝试调用函数时,我得到了这个错误。。。无重载方法“Hit”接受1个参数C# “无过载方法”;击中;接受1个参数。需要返回两个列表,c#,C#,在main中调用类函数时遇到问题,因为它需要同时返回两个列表。该函数将一张卡片从一个列表添加到另一个列表,然后将其从原始列表中删除。但是当我尝试调用函数时,我得到了这个错误。。。无重载方法“Hit”接受1个参数 using System; using System.Collections.Generic; using System.Text; namespace BlackJackGameX { public class MainClas
using System;
using System.Collections.Generic;
using System.Text;
namespace BlackJackGameX
{
public class MainClass
{
public static void Main (string[] args)
{
Deck Cards = new Deck();
Hand PlayerHand = new Hand ();
Console.WriteLine("Welcome to Black Jack\n\nPress Enter To Start");
Console.ReadLine ();
PlayerHand.Hit(PlayerHand);
PlayerHand.Hit(PlayerHand);
PlayerHand.HandPrint();
}
}
}
问题在这个Hand类的Hit函数的底部
using System;
using System.Collections.Generic;
using System.Text;
namespace BlackJackGameX
{
public class Hand
{
Deck CardDeck = new Deck ();
public List<Card> PlayerHand;
public Hand ()
{
}
public void HandPrint()
{
for (int i = 0; i < PlayerHand.Count; ++i)
{
Console.WriteLine("You have a " + PlayerHand[i].CardValue + " of " + PlayerHand[i].CardSuit);
if (i < PlayerHand.Count)
{
Console.WriteLine ("&");
}
}
Console.ReadLine();
}
public List<Card> Hit(List<Card> CardDeck, List<Card> PlayerHand)
{
PlayerHand.Add(CardDeck[1]);
CardDeck.Remove(CardDeck[1]);
return PlayerHand;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
命名空间BlackJackGameX
{
公务生
{
甲板甲板=新甲板();
公开名单玩家;
公众人物()
{
}
公开作废手印()
{
for(int i=0;i
出现的错误是因为类中的Hit()
方法接受2个参数,而不是1个。它需要两个List()
参数
您的代码正在传入一个手
对象,无论参数的数量如何,该对象都不会工作。您的命中
方法需要两个列表
参数,但您只向其传递一个手
对象
public List<Card> Hit(List<Card> CardDeck, List<Card> PlayerHand)
{
...
}
您的Main
如下所示:
public static void Main (string[] args)
{
Deck cards = new Deck();
Hand playerHand = new Hand(cards);
Console.WriteLine("Welcome to Black Jack\n\nPress Enter To Start");
Console.ReadLine();
playerHand.Hit();
playerHand.Hit();
// I would rename this to PrintHand(). HandPrint is a noun.
playerHand.HandPrint();
}
你为什么要退货?它们是引用(如指针),因此您不需要返回它们,它们已经被修改。要返回两个项目,通常使用Tuple
,但您的问题非常不清楚,无法真正推荐。正如您所看到的,我太深了,很抱歉发布此消息,我将看看我是否能找出哪里出了问题。那么我如何在主功能中利用我的其他类的列表?@JonDunn我在主功能中没有看到列表,只有手牌和牌组对象。牌组类是一个洗牌列表,手牌类包含一个玩家的牌组列表感谢帮助,但是我的代码仍然有效,建议如何在牌组中实现RemoveNext函数?如果牌组
类包含一个列表
,其中包含您用来表示牌的任何内容,并且牌组被洗牌(随机)了,您可以从牌组的顶部拖动<代码>var tmp=PlayerHand[0];PlayerHand.Remove(tmp);返回tmp
应该可以工作(或者使用RemoveAt(0);
。您好,很抱歉,我仍然在努力实现Hit功能,我已经尝试了我知道的任何方法,但似乎都不起作用,我应该将RemoveAt(0)放在哪里功能?在手牌类或牌组类中?当我想从牌组中取出一张牌,并将其添加到牌组中时,为什么我要从牌组中移除?这是我的错误;您想从牌组
对象中的牌列表中移除,然后返回该列表。用该列表的名称替换牌组
;方法是一样的。
public static void Main (string[] args)
{
Deck cards = new Deck();
Hand playerHand = new Hand(cards);
Console.WriteLine("Welcome to Black Jack\n\nPress Enter To Start");
Console.ReadLine();
playerHand.Hit();
playerHand.Hit();
// I would rename this to PrintHand(). HandPrint is a noun.
playerHand.HandPrint();
}