簡體   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