C# 索引超出范围。。。。但它没有';t似乎超出范围,所以发生了什么? 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 名称空间卡交易商 { 甲板 { 字符串[]组=新字符串[52]; 字符串[]名称=新字符串[13]; 字符串[]=新字符串[4]; 公共甲板() { 名称[0]=“Ace”; 名称[1]=“两个”; 名称[2]=“三”; 名称[3]=“四”; 姓名[4]=“五”; 名称[5]=“六”; 名称[6]=“七”; 名称[7]=“八”; 名称[8]=“九”; 名称[9]=“十”; 名称[10]=“未经检查”; 名称[11]=“Oberknave”; 姓名[12]=“国王”; 西装[0]=“红心”; 西服[1]=“钟声”; 西服[2]=“橡子的”; 西服[4]=“叶子的”; 对于(int i=0;i
嘿,伙计们,我的密码有问题。有两个类,一个构建甲板,另一个播放甲板。当我执行这段代码时,它抛出了一个错误(超出索引的范围),但我没有看到它,就像我不知道它是如何超出范围的一样,它向我显示了它超出范围的位置,但它似乎没有超出范围,所以发生了什么 您缺少索引3,而您有4个:C# 索引超出范围。。。。但它没有';t似乎超出范围,所以发生了什么? 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 名称空间卡交易商 { 甲板 { 字符串[]组=新字符串[52]; 字符串[]名称=新字符串[13]; 字符串[]=新字符串[4]; 公共甲板() { 名称[0]=“Ace”; 名称[1]=“两个”; 名称[2]=“三”; 名称[3]=“四”; 姓名[4]=“五”; 名称[5]=“六”; 名称[6]=“七”; 名称[7]=“八”; 名称[8]=“九”; 名称[9]=“十”; 名称[10]=“未经检查”; 名称[11]=“Oberknave”; 姓名[12]=“国王”; 西装[0]=“红心”; 西服[1]=“钟声”; 西服[2]=“橡子的”; 西服[4]=“叶子的”; 对于(int i=0;i,c#,indexoutofrangeexception,C#,Indexoutofrangeexception,嘿,伙计们,我的密码有问题。有两个类,一个构建甲板,另一个播放甲板。当我执行这段代码时,它抛出了一个错误(超出索引的范围),但我没有看到它,就像我不知道它是如何超出范围的一样,它向我显示了它超出范围的位置,但它似乎没有超出范围,所以发生了什么 您缺少索引3,而您有4个: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CardDealer
{
class Deck
{
string[] deck = new string[52];
string[] name = new string[13];
string[] suits = new string[4];
public Deck()
{
name[0] = "Ace ";
name[1] = "Two ";
name[2] = "Three ";
name[3] = "Four ";
name[4] = "Five ";
name[5] = "Six ";
name[6] = "Seven ";
name[7] = "Eight ";
name[8] = "Nine ";
name[9] = "Ten ";
name[10] = "Unterknave ";
name[11] = "Oberknave ";
name[12] = "King ";
suits[0] = "of Hearts";
suits[1] = "of Bells";
suits[2] = "of Acorns";
suits[4] = "of Leaves";
for (int i = 0; i < 13; i++)
{
deck[i] = name[i] + suits[0];
}
for (int i = 0; i < 13; i++)
{
deck[i + 13] = name[i] + suits[1];
}
for (int i = 0; i < 13; i++)
{
deck[i + 26] = name[i] + suits[2];
}
for (int i = 0; i < 13; i++)
{
deck[i + 39] = name[i] + suits[3];
}
}
Random rnd = new Random();
int cardsLeft = 52;
public void Shuffle()
{
string[] deck = new string[52];
for (int i = 0; i < 13; i++)
{
deck[i] = name[i] + suits[0];
}
for (int i = 0; i < 13; i++)
{
deck[i + 13] = name[i] + suits[1];
}
for (int i = 0; i < 13; i++)
{
deck[i + 26] = name[i] + suits[2];
}
for (int i = 0; i < 13; i++)
{
deck[i + 39] = name[i] + suits[3];
}
string[] myrandomarray = deck.OrderBy(x => rnd.Next()).ToArray();
deck = myrandomarray;
cardsLeft = 52;
}
public string Deal()
{
string deltCard = deck[0];
cardsLeft--;
string[] newDeck = new string[cardsLeft];
for (int i = 0; i < cardsLeft + 1; i++)
{
if (deck[i] != deltCard)
{
newDeck[0] = deck[i];
}
}
deck = newDeck;
return deltCard;
}
}
class play
{
static void Main()
{
Deck mydeck = new Deck();
Console.WriteLine(mydeck.Deal());
}
}
}
将最后一行更改为:
suits[0] = "of Hearts";
suits[1] = "of Bells";
suits[2] = "of Acorns";
suits[4] = "of Leaves";
西服[4]=“叶子的”
应该是
suits[3] = "of Leaves";
不要显式放置索引:
让编译器为您完成例行工作。与name
和deck
相同:
// create array with items mentioned
string[] suits = new string[] {
"of Hearts",
"of Bells",
"of Acorns",
"of Leaves"
};
//让我们去掉尾随空格
字符串[]名称=新字符串[]{
“王牌”,
“两个”,
“三”,
“四”,
“五个”,
“六”,
“七”,
“八”,
“九”,
“十”,
“无人值守的中堂”,
“Oberknave”,
“国王”,
};
//以及讨厌的“橡子”:适合“橡子”或“俱乐部”,而不是“橡子”
字符串[]适合=新字符串[]{
“红心”,
“钟声”,
“橡子”,
“树叶”,
};
弦[]甲板;
公共甲板(){
//名称和套装的所有组合(笛卡尔连接)
//避免使用像13这样的神奇数字(有些游戏如preference有一套8张牌)
甲板=名称
.SelectMany(n=>suits.Select(s=>$“{n}{s}”)/虽然您已经得到了问题的直接答案,但我可以提供一些代码改进,使您的生活更加轻松
- 每当使用for循环迭代数组时,首选
i
而不是i
- 要填充组,不要使用4个不同的循环,请使用嵌套循环
- 洗牌时不要填充新的牌组,而是创建现有牌组的副本并洗牌,或者简单地洗牌现有牌组
- 如果值是硬编码的,则更喜欢数组初始值设定项而不是循环
将所有这些点组合到代码中,更好的版本如下所示:
// Let's get rid of trailing spaces
string[] name = new string[] {
"Ace",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Unterknave",
"Oberknave",
"King",
};
// as well as pesky "of": suit "Acorns" or "Clubs", not "of Acorns"
string[] suits = new string[] {
"Hearts",
"Bells",
"Acorns",
"Leaves",
};
string[] deck;
public Deck() {
// All combinations of name and suits (cartesian join)
// Avoid magic numbers like 13 (some games like preference has 8 cards in a suit)
deck = name
.SelectMany(n => suits.Select(s => $"{n} of {s}")) // <- space and "of" here
.ToArray();
类甲板
{
//接下来的两个数组是静态的,因为它们总是相同的,
//没有必要为类的每个实例制作多个副本
私有静态字符串[]名称=新字符串[]
{
“A”,“2”,“3”,“4”/*这里更多的是相同的*/
};
私有静态字符串[]适合=新字符串[]
{
“红心”、“铃铛”、“橡子”、“树叶”
};
//此类中使用的所有字段
私有字符串[]组=新字符串[suits.Length*name.Length];
private int lastDelt=0;
私有随机rnd=新随机();
公共甲板()
{
//看看构造函数现在有多简单
对于(变量i=0;irnd.Next()).ToArray();
cardsLeft=甲板长度;
}
公共字符串处理()
{
如果(lastDelt>=甲板长度)
// create array with items mentioned
string[] suits = new string[] {
"of Hearts",
"of Bells",
"of Acorns",
"of Leaves"
};
// Let's get rid of trailing spaces
string[] name = new string[] {
"Ace",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Unterknave",
"Oberknave",
"King",
};
// as well as pesky "of": suit "Acorns" or "Clubs", not "of Acorns"
string[] suits = new string[] {
"Hearts",
"Bells",
"Acorns",
"Leaves",
};
string[] deck;
public Deck() {
// All combinations of name and suits (cartesian join)
// Avoid magic numbers like 13 (some games like preference has 8 cards in a suit)
deck = name
.SelectMany(n => suits.Select(s => $"{n} of {s}")) // <- space and "of" here
.ToArray();
class Deck
{
// The next two arrays are static since they are always the same,
// no point of making multiple copies of them for each instance of the class
private static string[] name = new string[]
{
"Ace", "Two", "Three", "Four" /* more of the same here*/
};
private static string[] suits = new string[]
{
"Hearts", "Bells", "Acorns", "Leaves"
};
// all the fields used in this class
private string[] deck = new string[suits.Length * name.Length];
private int lastDelt = 0;
private Random rnd = new Random();
public Deck()
{
// see how simple the constructor is now
for(var i = 0 ; i < suits.Length; i++)
{
for(var j = 0; j < name.Length; j++)
{
// Calculate deck index using i and j
deck[(i+1) * j] = name[j] +" of "+ suits[i];
}
}
}
public void Shuffle()
{
// You might want to look into the Fisher-Yates shuffle instead, link below.
deck = deck.OrderBy(x => rnd.Next()).ToArray();
cardsLeft = deck.Length;
}
public string Deal()
{
if(lastDelt >= deck.Length)
{
throw new Exception("No cards left in the deck");
}
string deltCard = deck[lastDelt];
lastDelt++;
return deltCard;
}
}