using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace while_test
{
    public class While_test
    {
        //定义while_test类中的Find方法,用于在数组中查找特定的值
        public static int Find(string keyValue,string[] array)
        {
            //定义整型数据i作为数组array的下标
            int i = array.Length - 1;
            while (i >= 0)
            {
                if (array[i] == keyValue)
                {
                    break;
                }
                i--;
            }
            //返回i值,找到即返回该元素在数组中的小标,否则返回-1
            return i;
        }
    }
    class Program
    {
        static void Main()
        {
            string[] a1 = { "张兰","苗群","洋洋","赵薇","王露","陈浩"};
            Console.WriteLine("输入你要查找的姓名");
            string findValue = Console.ReadLine();
            int arrayIndex = While_test.Find(findValue, a1);
            if (arrayIndex == -1)
            {
                Console.WriteLine("数组中无此人");
            }
            else
            {
                Console.WriteLine("此人在数组的第{0}个元素中",arrayIndex+1);
            }
            Console.ReadLine();
        }
    }
}


本文转载:CSDN博客