日期:2014-05-17  浏览次数:20526 次

数组取值问题
C# code

string[,] csys_a = new string[,] { { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }, { "白", "灰", "黄", "粉", "红", "紫", "绿", "蓝", "棕", "黑" } };



求助:我有这么个数组,如何根据 "A", "B", "C", "D", "E等得到后面的颜色


C# code


label.Text = "J" //这里的的J替换为相应的颜色。




------解决方案--------------------
C# code

void Main()
{
     
       string[] ay1={ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
       string[] ay2={ "白", "灰", "黄", "粉", "红", "紫", "绿", "蓝", "棕", "黑" } ;
       string key="J";
       string result=(from a in  ay1.Select((x,y)=>new {x,y})
                     join b in  ay2.Select((x,y)=>new {x,y})
                     on a.y equals b.y
                     where a.x==key
                     select b.x).FirstOrDefault();
        if(result !=null)
            Console.WriteLine(result); //黑
        else
            Console.WriteLine("No result found!");
}

------解决方案--------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication3
{
public partial class Form1 : Form
{
string[,] csys_a = new string[,] { { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }, { "白", "灰", "黄", "粉", "红", "紫", "绿", "蓝", "棕", "黑" } };
public Form1()
{
InitializeComponent();
}
//字母返回颜色,没有返回空
public string Switch(string Astr)
{
int k = csys_a.GetLength(1);
for (int i = 0; i < k ; i++)
{
if (csys_a[0, i] == Astr)
{
return csys_a[1, i];
}

}
return "";
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "F";
label1 .Text =Switch (label1 .Text );
}
}
}

这应该满足lz的要求