繁体   English   中英

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

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

编写一个程序来执行以下操作: 创建一个控制台应用程序,要求用户输入奥克兰和惠灵顿两个城市的郊区和邮政编码。 重复这 10 次(这意味着用户必须输入郊区和邮政编码,一个接一个 10 次)。 您需要使用 try 和 catch 块来处理异常。 将郊区的值存储在名为 suburb_values 的列表中,将邮政编码的值存储在名为 postcode_values 的列表中。 您现在需要询问用户是否要 INSERT()、REMOVE() 或 CONTAINS()。 这必须是一个开关盒。 如果用户选择 1:您需要将 5 个预定义的郊区和邮政编码值插入到相应的列表中,并在列表中打印最终值。 如果用户选择 2:您需要使用 Remove() 和 RemoveAt() 删除 4 个值。 如果用户选择 3:您需要检查 suburb_values 列表是否包含用户将输入的至少 5 个郊区。 如果用户选择任何其他值:您需要显示无效并退出。

到目前为止,这是我的代码

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();
    }
}

在这个阶段,我的代码几乎完成了,但是我如何检查 suburb_values 列表是否包含用户将输入的至少 5 个郊区。 ???

这是我的代码更新后仍然不确定是否 100% 正确但我正在尝试

使用系统; 使用 System.Collections.Generic; // 要获得列表,您需要使用 System.Collections.Generic 添加此行; 在使用系统的行下;

命名空间 Assessent1ListUsingGenerics {
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM