如何进行多个用户输入? [英] How do I take multiple user inputs?

查看:53
本文介绍了如何进行多个用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用数组(例如我在控制台中编写的代码)从1个texbox中获取多个用户输入。由于我是编程新手,我已经搜索了几个小时,我找不到任何东西。



我尝试过:



int i;

int size = 2;

string [] name = new string [size];

string [] age = new string [size];



Console.WriteLine(**输入姓名和年龄来查找记录* *);



for(i = 0; i< size; i ++)

{

Console.WriteLine(输入名称:);

name [i] = Console.ReadLine();



Console.WriteLine( 输入年龄:);

age [i] = Console.ReadLine();



}





Console.WriteLine(输入姓名或年龄来查找记录:);

string find = Console.ReadLine();



for (i = 0;我<尺寸; i ++)

{



if(find == name [i])

{

Console.WriteLine(姓名:+姓名[i] +年龄:+年龄[i]);



i ++;

}



否则if(find == age [i])

{





Console.WriteLine(年龄:+年龄[i] +姓名:+姓名[i]);

i ++ ;



}

how do i take multiple user inputs from 1 texbox using array like the code i wrote in console. As i am new to programming, i have searched for hours and i can't find anything.

What I have tried:

int i;
int size = 2;
string[] name = new string [size];
string[] age = new string[size];

Console.WriteLine(" **Enter name and age to find your record** ");

for (i = 0; i < size; i++)
{
Console.WriteLine("Enter Name: ");
name[i] = Console.ReadLine();

Console.WriteLine("Enter Age: ");
age[i] = Console.ReadLine();

}


Console.WriteLine("Enter name or age to find record:");
string find = Console.ReadLine();

for (i = 0; i < size; i++)
{

if (find == name[i])
{
Console.WriteLine("Name : " + name[i] + " Age : " + age[i]);

i++;
}

else if (find == age[i])
{


Console.WriteLine("Age :" + age[i] + " Name : " + name[i]);
i++;

}

推荐答案

 static   Dictionary<int, string> nameage = new Dictionary<int, string>();

        public static void Main(string[] args)
        {
           
            Console.WriteLine("enter number records do u need");
            int records = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < records; i++)
            {
                Console.WriteLine("Enter name and age");
                Console.WriteLine("Name  :");
                string name = Convert.ToString(Console.ReadLine());
                Console.WriteLine("Age :");
                int age = Convert.ToInt32(Console.ReadLine());
                nameage.Add(age, name);
            }
            switchcase();
            Console.WriteLine("do you want check it again if yes enter 1 else enter 2");
            int a =Convert.ToInt16( Console.ReadLine());
            if (a == 1)
            {
                switchcase();
            }
}



public static void switchcase()
        {
            Console.WriteLine("do u want to enter name select 1 else select 2");
            int select = Convert.ToInt16(Console.ReadLine());
            switch (select)
            {
                case 1:
                    Console.WriteLine("enter the name you want to search");
                    string name = Convert.ToString(Console.ReadLine());
                    foreach(KeyValuePair<int,string> d in nameage)
                    {
                        if (d.Value == name)
                        {
                            Console.WriteLine("the name is " + name + "  " + "the age is " + d.Key);
                            Console.ReadLine();
                        }
                    }

                    break;
                case 2:
                    Console.WriteLine("enter the age you want to search");
                   int age = Convert.ToInt16(Console.ReadLine());
                    foreach (KeyValuePair<int, string> d in nameage)
                    {
                        if (d.Key == age)
                        {
                            Console.WriteLine("the name is " + d.Value + "  " + "the age is " + d.Key); Console.ReadLine();
                        }
                    }

                    break;
                   
            }
        }


Dictionary<string, string> nameage = new Dictionary<string, string>();
       //this is for saving data what you enetered
       private void btn_Click(object sender, RoutedEventArgs e)
       {
           //tb textbox name of property :NAME
           //agetb is the textbox name of property : AGE
           //Here am considering tb and agetb separate textboxes ,names with spaces in tb textbox and age with spaces in agetb textbox

           string str = null;
           string[] strArrname = null;
           str = tb.Text;
           string agee = agetb.Text;
           char[] splitchar = { ' ' };
           strArrname = str.Split(splitchar);
           string[] strarrayage = agee.Split(splitchar);
           for (int name = 0; name < strArrname.Length ; name++)
           {
              for(int age=0; age ==name; age++)
               {
                   nameage.Add(strArrname[name],strarrayage[age]);
               }
           }
           tb.Text = "";agetb.Text = "";
       }
       //this is used to search if u enter name in tb textbox it displays name and age in agetb textbox
       private void search_Click(object sender, RoutedEventArgs e)
       {
           //here you enter name or age for searching
           string nameAGE = tb.Text;
           foreach (KeyValuePair<string, string> d in nameage)
           {
               if (d.Value == nameAGE)
               {
                   agetb.Text = "Name is " + "  " + d.Value + "  " + "Age is " + "   " + d.Key;
               }
               else if(d.Key == nameAGE)
               {
                   agetb.Text = "Name is " + "  " + d.Value + "  " + "Age is " + "   " + d.Key;

               }
           }

       }


这篇关于如何进行多个用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆