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

时间:2013-09-02 11:55:54

标签: c# .net arrays

 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);
   }
}

这不起作用。我怎样才能使它发挥作用。

2 个答案:

答案 0 :(得分:3)

你在找这样的东西吗?

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);
}

答案 1 :(得分:0)

你的意思是:

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

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