想要使用 if 验证给定数组的索引号

本文关键字:数组 索引 验证 if | 更新日期: 2023-09-27 18:29:57

 class Palette
 {  
      public static void Main()
      {    
           int array = new int [5];
           if (array[] = 0)
           {
                   Console.WriteLine("It's blue");
           }
           if (array[] = 1)
           {
                   Console.WriteLine("It's Violet");
           }
           if (array[] = 2)
           {
                   Console.WriteLine("It's red");
           }
           if (array[] = 3)
           {
                   Console.WriteLine("It's Black");
           }
           if (array[] = 4)
           {
                   Console.WriteLine("It's green");
           }
           Random ran = new Random();
           int r = ran.Next(array.Length);
           string Token = (array[r]);
           Console.WriteLine(Token);
   }
}

这是行不通的。我怎样才能让它工作。

想要使用 if 验证给定数组的索引号

你在寻找这样的东西吗?

public static void Main()
{
    string[] array = new string[5];
    array[0] = "It's blue";
    array[1] = "It's Violet";
    array[2] = "It's red";
    array[3] = "It's Black";
    array[4] = "It's green";
    Random ran = new Random();
    int r = ran.Next(array.Length);
    string token = array[r];
    Console.WriteLine(token);
}

你的意思是:

if (array[0] == "Blue") {
    Console.WriteLine("It's blue");
}
if (array[1] = "Violet") {
   Console.WriteLine("It's violet");
}