[英]What I'm trying to do is to generate an array of letters in alphabetical order, reverse alphabetical order, and print out the whole array randomly
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();
}
我期待它打印出 ab c defghijklmnopq r stuvwxyz 和同樣的東西,但也以相反的順序和隨機順序。
你不需要任何循環。
您已經確定了排序和反向排序,只需擺脫循環!
隨機位可以寫成 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));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.