C# 如何对数组进行随机循环
我想让代码变得简单:用英语单词,用单词的所有字符做随机数组。对于强度,单词“qweasdzxc”应该表示为:“adwseqzcx”(随机)。 因此,代码是:C# 如何对数组进行随机循环,c#,C#,我想让代码变得简单:用英语单词,用单词的所有字符做随机数组。对于强度,单词“qweasdzxc”应该表示为:“adwseqzcx”(随机)。 因此,代码是: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace randomLoop { class Program { static v
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace randomLoop
{
class Program
{
static void Main(string[] args)
{
string s = "qweasdzxc";
string[] q = newArray(s);
for (int i = 1; i < s.Length - 1; i++)
{
Console.Write(q[i]);
}
}
public static char[] getArrayofstring(string s)
{
char[] c = s.ToCharArray();
return c;
}
public static Random rnd = new Random();
public static string[] charAndNumber(char c, int lengthOftheWord)//0-char 1-integer
{
string[] array = new string[2];
array[0] = c.ToString();
array[1] = (rnd.Next(lengthOftheWord)).ToString();
return array;
}
public static string[] newArray(string s)
{
string[] c = new string[s.Length];
int j = 1;
string[] q = charAndNumber(s[j], s.Length - 1);
for (int i = 1; i < c.Length - 1; i++)
{
c[i] = "";
}
int e = 1;
while (e.ToString() != q[1])
{
c[e] = q[0];
j++;//for the character s[j] see up
e++;//for the loop
}
return c;
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间随机循环
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串s=“qweasdzxc”;
字符串[]q=新数组;
对于(int i=1;i
}您似乎只想对字符进行洗牌并生成一个新字符串。您可以使用使用洗牌算法的方法(根据答案,我根据您的情况对其进行了一些修改):
还请记住,这是一个扩展方法,因此您需要将其放入一个公共静态类。您到底在哪里?我看不出有问题……您还没有问任何问题!您刚刚发布了一些代码,这些代码看起来像是在试图解决问题(但很明显,它们甚至都没有接近,只需看看Main!),并且没有说明您在哪里感到困惑(除了有非工作代码)。我们需要更多的信息。不是开玩笑,但我们不会只为你编写程序!请解释你在哪里陷入困境/困惑/迷失,以及你的程序的下一步。你有3个函数,从我所看到的来看,它们都不互相调用,这看起来不像是解决问题的一致尝试。请帮助我们帮助您,让我们知道一些比“它不工作”更具体的信息。“qweasdzxc”不是一个英语单词。这是一个猫字,描述了当你的性器官被困在某物中时你发出的声音。在这种情况下,它感觉像是错过了“教一个人钓鱼”的部分,但OP并没有给我们在这方面提供太多的东西。
public static void Shuffle<T>(this T[] source)
{
Random rng = new Random();
int n = source.Length;
while (n > 1) {
n--;
int k = rng.Next(n + 1);
T value = source[k];
source[k] = source[n];
source[n] = value;
}
}
string s = "qweasdzxc";
char[] chars = s.ToCharArray();
chars.Shuffle();
string random = new string(chars);