你应该换种思路,改用hashtable存取数据,key里面存usename,value里面存password; 参照下面的代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace WindowsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string username="a"; string password="b"; System.Collections.Hashtable ht = new System.Collections.Hashtable(); ht.Add(username, password); ValidateArray(ht, "a", "b");
static void Main(string[] args)
{
int[,] arr = { { 1, 2, 3 }, { 2, 3, 4 } };
EnumerateArray(arr);
} static void EnumerateArray(Array array)
{
IEnumerator ie = array.GetEnumerator();
while (ie.MoveNext())
Console.WriteLine(ie.Current);
}
string[,] arr = new string[5,6];
for (int i = 0 ; i < arr.GetLength(0) ; i ++)
{
for (int j = 0 ; j < arr.GetLength(1) ; j ++)
{
string s = arr[i,j];
}
}
代码实现:/// <summary>
/// 验证用户
/// </summary>
/// <param name="_usersPassword">提供验证的用户与密码二维数组</param>
/// <param name="_userName">要验证的用户名</param>
/// <param name="_userPassword">要验证的用户密码</param>
/// <returns>
/// 0:无法找到该用户!
/// 1:密码错误!
/// 2:成功验证!
/// </returns>
private int ValidateArray(Array _usersPassword, string _userName, string _userPassword) {
System.Collections.IEnumerator myIEnumerator = _usersPassword.GetEnumerator();
int numRank = _usersPassword.Rank;
// 数组的下标
int count = 1;
while (myIEnumerator.MoveNext() & count == 1) {
// 判断用户名,因为是用户名跟密码的一组对比,所以要控制下下标,
if (string.Compare(myIEnumerator.Current.ToString(), _userName, true) == 0 & count == 1) {
count++;
// 判断用户密码
// 因为是用户名跟密码的一组对比,所以要控制下下标,
if (myIEnumerator.MoveNext() & count == numRank) {
if (string.Compare(myIEnumerator.Current.ToString(), _userPassword, true) == 0) return 2;
else return 1;
}
}
}
return 0;
}现在问题是假如我要验证一个多维数组中,当前维数的下标相等.于是,我每增加一个下标,都要在代码上增加一个if语句,假设我有很多个下标要验证,该如何实现? 或许我的思维有点问题.
请各位赐教! 谢谢
参照下面的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
string username="a";
string password="b";
System.Collections.Hashtable ht = new System.Collections.Hashtable();
ht.Add(username, password);
ValidateArray(ht, "a", "b");
} /// <summary>
/// 验证用户
/// </summary>
/// <param name="_usersPassword">提供验证的用户与密码二维数组</param>
/// <param name="_userName">要验证的用户名</param>
/// <param name="Password">要验证的用户密码</param>
/// <returns>
/// 0:无法找到该用户!
/// 1:密码错误!
/// 2:成功验证!
/// </returns>
private int ValidateArray(System.Collections.Hashtable _usersPassword, string _userName, string Password)
{
if (_usersPassword.ContainsKey(_userName) == false)
{
return 0;
} if ((string)_usersPassword[_userName] == Password)
{
return 2;
}
else
{
return 1;
} } }
}
{
int[,] arr = { { 1, 2, 3 }, { 2, 3, 4 } };
EnumerateArray(arr);
}static void EnumerateArray(Array array)
{
foreach(object v in array)
{
Console.WriteLine(v);
}
}借一楼的例子,其实和一楼是一回事,但看起来舒服点