简体   繁体   English

我如何使用多条目循环将用户输入的字符串值存储在列表中 c#

[英]how do i store a string value from user input in a list using loop for multiple entry c#

Write a program to do the following: Create a console application which asks user to enter suburb and postcode for two cities Auckland and Wellington.编写一个程序来执行以下操作: 创建一个控制台应用程序,要求用户输入奥克兰和惠灵顿两个城市的郊区和邮政编码。 Iterate this 10 times (which means the user has to input suburb and postcode, one after the other 10 times).重复这 10 次(这意味着用户必须输入郊区和邮政编码,一个接一个 10 次)。 You are required to handle the exceptions using try and catch block.您需要使用 try 和 catch 块来处理异常。 Store the values of suburb in a List called suburb_values and the values of the postcodes in a List called postcode_values.将郊区的值存储在名为 suburb_values 的列表中,将邮政编码的值存储在名为 postcode_values 的列表中。 You are now required to ask the user if they want to INSERT(), REMOVE() or CONTAINS().您现在需要询问用户是否要 INSERT()、REMOVE() 或 CONTAINS()。 This must be a switch case.这必须是一个开关盒。 If the user selects 1: you are required to INSERT 5 pre-defined suburb and post code values into the respective lists and print the final values inside the list.如果用户选择 1:您需要将 5 个预定义的郊区和邮政编码值插入到相应的列表中,并在列表中打印最终值。 If the user selects 2: you are required to REMOVE 4 values using Remove() and RemoveAt().如果用户选择 2:您需要使用 Remove() 和 RemoveAt() 删除 4 个值。 If the user selects 3: you are required to check if the suburb_values list contains atleast 5 suburbs that the user will enter.如果用户选择 3:您需要检查 suburb_values 列表是否包含用户将输入的至少 5 个郊区。 If the user selects any other value: you need to display invalid and exit.如果用户选择任何其他值:您需要显示无效并退出。

THIS IS MY CODE TILL NOW到目前为止,这是我的代码

using System;
using System.Collections.Generic;       // TO HAVE LIST YOU NEED TO ADD THIS LINE using System.Collections.Generic; UNDER the line using System;

namespace Assessent1ListUsingGenerics
{       
class Program                               //List<T> in C# | In Class Activity

{                                           //a. WAP to insert 10 values of Country and capital names and
                                            //its pin code (two lists: suburb<string> and pincode<int>).
                                            //Print and display the list.
    static void Main(string[] args)
    {
        int counter = 0;
        var suburb_values = new List<string>();
        while (counter < 10) 
        {
            //creating list suburb_ values ( String Type )
            //Adding values to the list from user input                        //user input will be saved in country_values

            Console.WriteLine("PLease Enter a Suburb for Auckland City");
            suburb_values.Add(Console.ReadLine());     //suburb_values 1        //this will be the index 2 correct ?            
            suburb_values.Add(Console.ReadLine());     //suburb_values 2            
            suburb_values.Add(Console.ReadLine());     //suburb_values 3            country_values.Add(Console.ReadLine());     //country name 1            
            suburb_values.Add(Console.ReadLine());     //suburb_values 4            
            suburb_values.Add(Console.ReadLine());     //suburb_values 5            
            suburb_values.Add(Console.ReadLine());     //suburb_values 6    
            suburb_values.Add(Console.ReadLine());     //suburb_values 7            
            suburb_values.Add(Console.ReadLine());     //suburb_values 8            
            suburb_values.Add(Console.ReadLine());     //suburb_values 9            
            suburb_values.Add(Console.ReadLine());     //suburb_values 10
            counter;
        } 
       
        //creating list postcode_values ( String Type )
        var postcode_values = new List<string>();
        Console.WriteLine("Please enter a Post code for the Auckland Suburb you entered ");
        //Adding values to the list
        postcode_values.Add(Console.ReadLine());    //postcode 1
        postcode_values.Add(Console.ReadLine());    //postcode 2  
        postcode_values.Add(Console.ReadLine());    //postcode 3
        postcode_values.Add(Console.ReadLine());    //postcode 4
        postcode_values.Add(Console.ReadLine());    //postcode 5
        postcode_values.Add(Console.ReadLine());    //postcode 6
        postcode_values.Add(Console.ReadLine());    //postcode 7
        postcode_values.Add(Console.ReadLine());    //postcode 8
        postcode_values.Add(Console.ReadLine());    //postcode 9    
        postcode_values.Add(Console.ReadLine());    //postcode 10

        Console.WriteLine("PLEASE CHOOSE TO INSERT - REMOVE _ CONTAINS \n");
        Console.WriteLine("PLEASE SELECT ON OPTION: ");
        Console.WriteLine("\nPress 1 for Insert - Press 2 for Remove - Press 3 for Contains ");

        int userInput = Convert.ToInt32(Console.ReadLine());  //ASK YOUR INPUT WITH THIS CODE, what user type will be saved inside userInput

        switch (userInput)
        {
            case 1:  //you are required to INSERT 5 pre-defined suburb and post code values into the respective lists and print the final values inside the list. ");
                {

                    Console.WriteLine("\nOption 1 Selected - PLEASE INSERT 2 SUBURBS AND 2 POSTCODES FOR AUCKLAND CITY");
                    //Inserting values to the Suburb list
                    Console.WriteLine("Please Insert a Suburb ");
                    suburb_values.Insert(0, Console.ReadLine());
                    Console.WriteLine("Please Insert the next Suburb ");
                    suburb_values.Insert(1, Console.ReadLine());

                    //inserting values to the PostCode list
                    Console.WriteLine("Please Inser a PostCode");
                    postcode_values.Insert(0, Console.ReadLine());
                    Console.WriteLine("Please Inser a PostCode");
                    postcode_values.Insert(1, Console.ReadLine());

                    break;
                }

            case 2:
                {
                    Console.WriteLine("\nOption 2 selected - you are required to REMOVE 4 values using Remove() and RemoveAt().D");
                    suburb_values.Remove("");
                    suburb_values.RemoveAt(0);
                    break;
                }
            case 3:
                {
                    Console.WriteLine(" you  are  required  to  check  if  the  suburb_values  list  contains  atleast  5 suburbs that the user will enter. ");
                    // code to check missing
                    break;
                }
            default:
                Console.WriteLine("Invalid");
                break;
        }
        Console.ReadKey();
    }
}

At this stage i have the code almost done but how do I check if the suburb_values list contains atleast 5 suburbs that the user will enter.在这个阶段,我的代码几乎完成了,但是我如何检查 suburb_values 列表是否包含用户将输入的至少 5 个郊区。 ??? ???

here is my code updated still not sure if is 100% correct but I'm trying这是我的代码更新后仍然不确定是否 100% 正确但我正在尝试

using System;使用系统; using System.Collections.Generic;使用 System.Collections.Generic; // TO HAVE LIST YOU NEED TO ADD THIS LINE using System.Collections.Generic; // 要获得列表,您需要使用 System.Collections.Generic 添加此行; UNDER the line using System;在使用系统的行下;

namespace Assessent1ListUsingGenerics {命名空间 Assessent1ListUsingGenerics {
class Program //List in C# | class Program //列表在C# |

{                                           //a. insert 10 values of suburb_values  and postcode_values
                                            

    static void Main(string[] args)
    {   //------------------------------------ AUCKLAND---------------------------------------------------------------

        int counter = 0; // this is for the loop to execute asking user input 10x times for each suburb and postcode values to store in the variables 
        
        var suburb_values = new List<string>(); //creating list suburb_ values ( String Type )
        
        var postcode_values = new List<string>(); //creating list postcode_values ( String Type )
        try
        {
            while (counter < 10)  //counter is zero and won't pass the limit of 10 times if the user input some value
            {
                //Adding values to the list from user input                        //user input will be saved in suburb_values

                Console.WriteLine("Please enter a Suburb for Auckland City: ");
                suburb_values.Add(Console.ReadLine());     //suburb_values 1        //this will be the index 2 correct ?            
                                                           //Console.Clear();

                Console.WriteLine("Please enter a Post code number: ");
                //Adding values to the list
                postcode_values.Add(Console.ReadLine());    //postcode 1
                                                            // Console.Clear();
                counter++;
            }
        }
       
        catch (FormatException e)
        {
            Console.WriteLine(e.Message);
        }

        finally
        {
            Console.WriteLine(""); //This is Finally block and get executed regardless
        }

        //--------------FROM HERE SWITCH LOOP FOR AUCKLAND-------------------------------------------------------

        Console.WriteLine("\nPLEASE SELECT 1 TO INSERT - 2 TO REMOVE - OR 3 TO CHECK ELEMENTS IN THE LIST CONTAINS \n");            


        int userInput = Convert.ToInt32(Console.ReadLine());  //ASK YOUR INPUT WITH THIS CODE, what user type will be saved inside userInput


        switch (userInput)
        {   
            case 1:  //you are required to INSERT 5 pre-defined suburb and post code values into the respective lists and print the final values inside the list. ");
                {
                    try
                    {
                        Console.WriteLine("\nOption 1 Selected - PLEASE INSERT 5 SUBURBS AND POSTCODES FOR AUCKLAND AND WELLINGTON CITY");

                        //Inserting new values to the suburb_values list and postcode list 
                        Console.WriteLine("PLease insert a new Suburb for Auckland City: ");
                        suburb_values.Insert(0, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please insert a new PostCode for the Aucland city: ");
                        postcode_values.Insert(0, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please insert the next Suburb for Auckland City: ");
                        suburb_values.Insert(1, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please Insert the next PostCode for Aucklanf city:");
                        postcode_values.Insert(1, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please insert the next Suburb for Auckland City: ");
                        suburb_values.Insert(2, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please Insert the next PostCode for Aucklanf city:");
                        postcode_values.Insert(2, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please insert the next Suburb for Auckland City: ");
                        suburb_values.Insert(3, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please Insert the next PostCode for Aucklanf city:");
                        postcode_values.Insert(3, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please insert the next Suburb for Auckland City: ");
                        suburb_values.Insert(4, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please Insert the next PostCode for Aucklanf city:");
                        postcode_values.Insert(4, Console.ReadLine());
                        //Console.Clear();
                    }
                    catch (FormatException e)
                    {
                        Console.WriteLine(e.Message);
                    }

                    finally
                    {
                        Console.WriteLine(""); //This is Finally block and get executed regardless
                    }


                    Console.WriteLine("Final Values Inside the Lists: ");
                    //printing the list
                    foreach (var add in suburb_values)
                        Console.Write(add + ": ");
                    //printing the list
                    foreach (var add in postcode_values)
                        Console.WriteLine(add);


                    break;
                }

            case 2:
                {   //REMOVING THE ELEMENT FROM THE LIST USING AT
                    Console.WriteLine("\nOption 2 selected - ");
                    try
                    {
                        int removing = 0;

                        while (removing < 4)             //There is 10 positions on the list but here user will remove only 4   //userinput position 0
                                                                                                                                //userinput position 1
                        {                                                                                                       //userinput position 2
                            Console.WriteLine("\nPLEASE ENTER A NUMBER TO DELETE THE SUBURB ON THE LIST\n");                    //userinput position 3
                            suburb_values.RemoveAt(Convert.ToInt32(Console.ReadLine()));                                        //userinput position 4
                                                                                                                                //userinput position 5
                            foreach (var sub in suburb_values)                                                                  //userinput position 6
                            {                                                                                                   //userinput position 7
                                Console.WriteLine(sub);                                                                         //userinput position 8
                                //removing++;                                                                                   //userinput position 9
                            }                                                                                       
                            removing++;
                        }

                    }
                    catch (FormatException e)
                    {
                        Console.WriteLine(e.Message);
                    }

                    finally
                    {
                        Console.WriteLine(""); //This is Finally block and get executed regardless
                    }
                    //suburb_values.RemoveAt(0);
                    break;
                }
            case 3:
                {
                    Console.WriteLine(" you  are  required  to  check  if  the  suburb_values  list  contains  atleast  5 suburbs that the user will enter. ");
                    // code to check missing
                    //check list here 
                    Console.WriteLine("Checking List values not index number 8 :{0}", suburb_values.Contains("")); // Cheking is 8 in the pincode list or not.
                    Console.WriteLine("Checking values Grey Lynn exist in the subrub list :{0}", suburb_values.Contains("Grey Lynn"));
                    break;
                }
            default:
                Console.WriteLine("Invalid");
                break;
        }
        Console.ReadKey();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 如何将字符串值存储在循环输出的列表中 - C# How to store string values in a list outputted from a loop 我正在尝试在 while 循环中获取一个 int 列表以将用户输入存储在 c# 中 - I'm trying to get a int list in a while loop to store user input in c# C#需要建立一个循环来拒绝无效的用户输入(低于5的1 /以下)我该怎么做? 编码新手,制作电影院入门应用程序 - C# Need to set up a loop to reject invalid user input (below 1/ above 5) How can I do that? New to coding, making cinema entry app 如何从用户输入比较 C# 中的日期? - How do I compare dates in C# from User Input? 如何将文本框条目放入while循环? C# - How do I put a textbox entry into while loop? C# 如何将EditText用户输入存储到c#中的字符串变量中 - how to store EditText user input into a string variable in c# 从列表框中取出数据时,如何从C#中的字符串计算值? - How do I calculate a value from a string in C# when taking data out of a list box? 如何使用循环从c#中的字符串中提取特定的子字符串? - how do I extract a specific substring from a string in c# using a loop? 如何将用户输入的字符串存储在数组中以在 C# 中对其执行搜索操作? - How to store a string from user input in an array to perform search operation on it in C#? 显示来自 do while 循环 C# 的用户输入 - display user input from do while loop C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM