我试图做的是按字母顺序生成一个字母数组,反向按字母顺序,然后随机打印出整个数组

  • 本文关键字:顺序 数组 打印 随机 然后 一个 c# console
  • 更新时间 :
  • 英文 :

namespace Practice69
{
class Program
{
static void Main(string[] args)
{
string[] alphabet;
alphabet = new string[] { "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", "z", "x", "c", "v", "b", "n", "m" };
// making the keyboard layout alphabetical and print horizontally
Array.Sort(alphabet);
for (int i = 0; i < 1; i++)
{
Console.WriteLine(String.Join(" ", alphabet), alphabet[i]);
}
// making the alphabet print out reverse alphabetical and horizontally 
Array.Reverse(alphabet);
for (int i = 0; i < 1; i++)
{
Console.WriteLine(String.Join(" ", alphabet), alphabet[i]);
}
// making the whole alphabet print out radomly
Random randAlphabet = new Random();
for (int i = 0; i < 1; i++)
{
Console.WriteLine(String.Join(" ", alphabet), alphabet[i]);
foreach (string value in alphabet)
{
randAlphabet.Next();
}

我希望它能打印出一个b/c,然后输出一个b/c,然后输出一个b/c同样的东西,但顺序相反,而且是随机的。

  1. 你不需要任何循环

  2. 你已经有排序和反向排序钉,只是摆脱循环!

  3. 随机位可以写成LINQ查询(一个秘密循环!)

这个怎么样?

class Program
{
static void Main(string[] args)
{
char[] alphabet;
alphabet = new char[] { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm' };
// making the keyboard layout alphabetical and print horizontally
Array.Sort(alphabet);
Console.WriteLine(string.Join(' ', alphabet));
// making the alphabet print out reverse alphabetical and horizontally 
Array.Reverse(alphabet);
Console.WriteLine(string.Join(' ', alphabet));
// making the whole alphabet print out randomly
Random randAlphabet = new Random();
alphabet = alphabet.OrderBy(x => randAlphabet.Next()).ToArray();
Console.WriteLine(string.Join(' ', alphabet));
}
}

相关内容

  • 没有找到相关文章

最新更新