【发布时间】:2017-07-06 14:22:17
【问题描述】:
我已经声明了一个字符串数组,我想与用户给出的名称进行比较,
string[] MasterList = new string[] {
"Askay", "Puram", "Raman", "Srinivasa",
"Gopal", "Rajesh", "Anju", "Nagara",
};
string YourName;
Console.WriteLine("Enter your name: ");
YourName = Console.ReadLine();
for(i=0; i<5; i++)
{
a = String.Compare(MasterList[i], YourName);
Console.WriteLine("Your name is not among the list")
}
结果输出不是我所期望的,有什么想法可以解决吗?
【问题讨论】:
-
当您的数组中有超过 5 个索引时,为什么要使用
i<5?为什么不MasterList.Length? -
您的代码无法编译,您还没有说出您的期望。但很简单:
if(!MasterList.Contains(YourName)){...}而不是 for 循环。