我编写代码来打乱单词我正在创建简单的游戏混乱
string jumble = theWord;
int length = jumble.Count();
for (int i = 0; i < length; ++i)
{
int index1 = (rand.Next() % length);
int index2 = (rand.Next() % length);
char temp =jumble[index1];
jumble = jumble.Replace(jumble[index1], jumble[index2]);
jumble = jumble.Replace(jumble[index1], temp);
}
更新代码
string jumble = theWord;
int length = jumble.Count();
for (int i = 0; i < length; ++i)
{
int index1 = (rand.Next() % length);
//int index2 = (rand.Next() % length);
char temp = jumble[index1];
jumble[i] = jumble[index1 - 1];
jumble[i] = temp;
}
错误 1 属性或索引器 'string.this[int]' 无法分配给 - 它是只读的