【问题标题】:storing user info to an array for use in a switch statment将用户信息存储到数组中以在 switch 语句中使用
【发布时间】:2021-03-09 16:04:06
【问题描述】:

我的任务是菜单中的选项,我目前正试图将用户输入(在本例中为带有 T 恤的品牌和尺寸的字符串)存储到一个数组中,以便稍后在某些情况下使用其他菜单选项。

namespace A4pf
{
    class Program
    {
        static void Main(string[] args)
        {
            int option = 0;

            Console.WriteLine("1. Add New T-shirt Details");
            Console.WriteLine("2. Edit Exisiting T-shirt detials");
            Console.WriteLine("3. Display All T-shirts in store");
            Console.WriteLine("4. Delete T-shirt information");
            Console.WriteLine("5. Exit");

            switch(option)
            {
                case 1:
                    Console.WriteLine("Please enter the T-Shirts Details. ");
                    Console.Write("Brand Name and size(eg. Thrasher-M");
                    string[] tshirtDetails = new string[12];

                    for (int i = 0; i < tshirtDetails.Length; i++)
                    {
                        tshirtDetails[i] = Console.ReadLine();
                    }
                    
                    break;
                default:
                    
                    break;
                  
            }
            Console.ReadLine();
        } 
     
    }
}

【问题讨论】:

  • 您需要将数组移出 switch 语句。这将使它在每个菜单选项中都可用,因为它的范围更大。
  • 您还可以使用其他类型的集合,例如列表或字典,并将详细信息存储在称为衬衫的新类对象中。
  • 是的,但作业特别要求我使用数组
  • 提示:option需要在switch语句前设置。字符串输入也需要解析为整数才能使用。
  • 请描述什么衬衫细节。您有 12 个字符串的空间,但帖子中没有关于这些字符串的信息。

标签: c# arrays .net


【解决方案1】:

您在 switch case 1 中创建一个数组,因此每次输入 case 1 时,都会创建新数组,因此之前的数据将被删除。我建议您尝试将该行移到int option = 0 上方,以便在其他情况下也可以使用数组中的当前数据,或者如果要删除以前的数据,至少将数组的声明移到开关盒之外当您进入案例 1 时。

【讨论】:

    【解决方案2】:

    正如其他人所说,您的数组范围太窄(小)。

    假设你和 4 个人住在一起。你们每个人都有自己的房间,你们都共享一个共同的空间——厨房。厨房里面是一个装有食物的冰箱。你们谁都不能进入彼此的房间。室友 1 号决定把冰箱放在他的房间里。现在没有其他人可以接触到冰箱里的食物,因为其他室友不被允许进入他的房间。本质上,这就是将数组声明放在案例#1 中时发生的情况。对于每个室友来说,冰箱需要在一个共同的地方——厨房。当您将“{”和“}”与“if”、“for”、“switch-case”等语句一起使用时,您正在定义一个范围(即:一个房间)。最好只提供所需的访问权限,但同时确保需要访问变量(例如数组)的所有内容都可以访问它。

    您是否被介绍/教过编写伪代码?写下您需要执行的步骤。适当时使用普通单词和 C# —— 编写伪代码时不需要使用正确的 C# 语法,但有时会有所帮助。最初,从高层开始。然后修改它,直到你把这些步骤分解得足够远,你可以开始编程了。您决定细节是什么,以及需要多少细节。

    这是一个伪代码示例:

    修订 #1:

    Inform user what the program does (Console.WriteLine).
    
    Give user 5 options. If user enters an invalid option, re-prompt user. Display these options until user selects option #5 (loop / Console.WriteLine)
    1. Add New T-shirt Details
    2. Edit Exisiting T-shirt detials
    3. Display All T-shirts in store
    4. Delete T-shirt information
    5. Exit
    
    Get user response (Console.ReadLine)
    
    if userResponse = 1 then add new t-shirt details
    else if userResponse = 2 then edit existing t-shirt details
    else if userResponse = 3 then display all t-shirts in store
    else if userResponse = 4 then delete t-shirt information
    else if userResponse = 5 then exit the program
    else user selected an option that doesn't exist
    

    现在我们已经大致了解了需要做什么,我们可以在不太清楚的部分添加更多细节。

    修订 #2:

    Inform user what the program does (Console.WriteLine).
    
    Give user 5 options. If user enters an invalid option, re-prompt user. Display these options until user selects option #5 (loop / Console.WriteLine)
    1. Add New T-shirt Details
    2. Edit Exisiting T-shirt details
    3. Display All T-shirts in store
    4. Delete T-shirt information
    5. Exit
    
    Get user response (Console.ReadLine)
    Save user response in integer variable (name: option)
    

    查看 5 个选项后,我注意到我将向用户询问更多信息(T 恤详细信息)。我将如何存储 T 恤的详细信息?我会将它们存储在一个字符串变量中。我需要存储多件 T 恤的详细信息。哪些数据类型允许我存储多个值?我将使用一个数组——一个字符串数组。 5 个选项中有多少需要访问 T 恤详细信息?不止一个,所以我会确保他们都可以访问这个数组。用户将输入多少件 T 恤的详细信息?我不确定——用户可以输入多少他/她想输入多少。我将从 1 开始并根据需要调整数组的大小。

    string[] tshirtDetails = new string[1];
    

    让我们继续添加更多细节。我们将首先为选项 #1 添加更多细节。

    我需要获取 T 恤的详细信息。我怎样才能让用户知道?

    if userResponse = 1 then add new t-shirt details
    {
        prompt user for t-shirt details (Console.WriteLine)     
        get t-shirt details from user (Console.ReadLine)
        store t-shirt details in array (tshirtDetails) 
    }
    

    让我们继续选项 #2。我需要让用户编辑现有的 T 恤详细信息。他/她如何知道存在哪些 T 恤?我应该在选择此选项时显示 T 恤详细信息,还是用户应在选择此选项之前显示现有 T 恤?如果需要,用户可以使用选项 #3 来显示现有的 T 恤信息。

    else if userResponse = 2 then edit existing t-shirt details
    {
        prompt user for which t-shirt details to modify (Console.WriteLine)     
        get t-shirt index from user (Console.ReadLine)
        update t-shirt details in array (tshirtDetails) 
    }
    

    接下来,我们将为选项 #3 添加更多详细信息。由于我们使用的是数组,因此我们必须从变量中获取多个值。我们将使用循环。 “for”(或“foreach”)应该可以工作。

    else if userResponse = 3 then display all t-shirts in store
    {
       for each detail in tshirtDetails Console.WriteLine
    }
    

    继续选项#4。我需要删除哪个 T 恤细节?我需要询问用户。

    else if userResponse = 4 then delete t-shirt information
    {
        prompt user for which t-shirt details to modify (Console.WriteLine)     
        get t-shirt index from user (Console.ReadLine)
        remove t-shirt details from array (tshirtDetails)     
    }
    

    选项 #5。这个是不言自明的,所以我不需要在这里添加更多细节。

    else if userResponse = 5 then exit the program
    

    如果用户输入了一个无效的选项怎么办?我会通知他们,然后重新提示一个有效的选项。

    else 
        inform user of invalid option (Console.WriteLine)
    

    修订 #3:

    Inform user what the program does (Console.WriteLine).
    
    Give user 5 options. If user enters an invalid option, re-prompt user. Display these options until user selects option #5 (loop / Console.WriteLine) 
    1. Add New T-shirt Details
    2. Edit Exisiting T-shirt details
    3. Display All T-shirts in store
    4. Delete T-shirt information
    5. Exit
    
    Get user response (Console.ReadLine)
    Save user response in integer variable (name: option)
    
    string[] tshirtDetails = new string[1];
    
    
    if userResponse = 1 then add new t-shirt details
    {
        prompt user for t-shirt details (Console.WriteLine)     
        get t-shirt details from user (Console.ReadLine)
        store t-shirt details in array (tshirtDetails) 
    }
    

    “在数组中存储 T 恤详细信息”。我的数组大小是 1,如果用户输入三件 T 恤的详细信息会怎样?在将每个 T 恤细节添加到数组之前,我最好调整数组的大小。让我们在“将 T 恤详细信息存储在数组 (tshirtDetails)”之前为选项 #1 添加一些伪代码

    if userResponse = 1 then add new t-shirt details
    {
        prompt user for t-shirt details (Console.WriteLine)     
        get t-shirt details from user (Console.ReadLine)
        resize array (tshirtDetails)
        store t-shirt details in array (tshirtDetails) 
    }
    

    我们继续查看伪代码:

    else if userResponse = 2 then edit existing t-shirt details
    {
        prompt user for which t-shirt details to modify (Console.WriteLine)     
        get t-shirt index from user (Console.ReadLine)
        update t-shirt details in array (tshirtDetails) 
    }
    
    else if userResponse = 3 then display all t-shirts in store
    {
       for each detail in tshirtDetails Console.WriteLine
    }
    
    else if userResponse = 4 then delete t-shirt information
    {
        prompt user for which t-shirt details to modify (Console.WriteLine)     
        get t-shirt index from user (Console.ReadLine)
        remove t-shirt details from array (tshirtDetails)     
    }
    
    else if userResponse = 5 then exit the program
    
    else 
        inform user of invalid option (Console.WriteLine)
    

    我想我想使用“switch-case”语句而不是“if-else if-else”语句。

    修订 #4:

    Inform user what the program does (Console.WriteLine).
    
    Give user 5 options. If user enters an invalid option, re-prompt user. Display these options until user selects option #5 (loop / Console.WriteLine)
    1. Add New T-shirt Details
    2. Edit Exisiting T-shirt details
    3. Display All T-shirts in store
    4. Delete T-shirt information
    5. Exit
    
    Get user response (Console.ReadLine)
    Save user response in integer variable (name: option)
    
    string[] tshirtDetails = new string[1];
    
    switch(option)
    {
        case 1:  
            //add new t-shirt details
        
            prompt user for t-shirt details (Console.WriteLine)     
            get t-shirt details from user (Console.ReadLine)
            resize array (tshirtDetails)
            store t-shirt details in array (tshirtDetails) 
    
        case 2:  
            //edit existing t-shirt details 
        
            prompt user for which t-shirt details to modify (Console.WriteLine)     
            get t-shirt index from user (Console.ReadLine)
            update t-shirt details in array (tshirtDetails) 
        
        case 3:  
            //display all t-shirts in store
    
            for each detail in tshirtDetails 
            {
               Console.WriteLine t-shirt details
            }
    
        case 4:  
            //delete t-shirt information
        
            prompt user for which t-shirt details to modify (Console.WriteLine)     
            get t-shirt index from user (Console.ReadLine)
            remove t-shirt details from array (tshirtDetails)    
        
        case 5:  
            //exit
            exit the program
    
        default:  
            //user selected an option that doesn't exist
        
            inform user of invalid option (Console.WriteLine)
    }
    

    我很高兴我有足够的细节来开始编程。

    namespace A4pf
    {
        class Program
        {
            static void Main(string[] args)
            {
                int option = 0;
                string optionStr = string.Empty;
                string[] tshirtDetails = new string[1];
    
                Console.WriteLine("Welcome to the T-shirt Inventory Management program.\n");
    
                do
                {
                    //re-initialize
                    option = 0;
    
                    Console.WriteLine("1. Add New T-shirt Details");
                    Console.WriteLine("2. Edit Exisiting T-shirt detials");
                    Console.WriteLine("3. Display All T-shirts in store");
                    Console.WriteLine("4. Delete T-shirt information");
                    Console.WriteLine("5. Exit");
    
                    Console.Write("\nSelect an option: ");
                    optionStr = Console.ReadLine();
    
                    //convert to int
                    Int32.TryParse(optionStr, out option);
    
                    switch (option)
                    {
                        case 1:
                            //add new t-shirt details
                            Console.Write("Please enter the T-Shirts Details. ");
                            Console.Write("Brand Name and size(eg. Thrasher-M): ");
    
                            //ToDo: add code
    
                            Console.WriteLine("\nT-shirt details saved.\n");
                            break;
                        case 2:
                            //edit existing t-shirt details 
    
                            //ToDo: add code
    
                            Console.WriteLine("\nT-shirt details updated.\n");
    
                            break;
                        case 3:
                            //display all t-shirts in store
                                
                            //ToDo: add code
    
                            break;
                        case 4:
                            //delete t-shirt information
    
                            //ToDo: add code
    
                            Console.WriteLine("\nT-shirt details deleted.\n");
    
                            break;
                        //case 5:
                            //exit
    
                        //    break;
                        default:
                            //user selected an option that doesn't exist
    
                            Console.WriteLine("\nError: Invalid option entered (" + optionStr + "). Please try again.\n");
                            break;
                    }
    
                    
    
                } while (option != 5);
            }
        }
    }
    

    您可能会注意到不需要“Case 5”,因为我们在“while”语句中评估 if option = 5。我已经注释掉了。由于“案例 5”及其包含的代码不是必需的,因此可以将其删除(移除)。

    注释//ToDo: add code表示您可能需要添加额外的C#代码。

    【讨论】:

      【解决方案3】:

      由于这是C#,您不妨从一些类构建开始来存储信息。请参阅下面的类 Shirt 以及一些基本功能

      public class Shirt
      {
          public Shirt()
          {
              Brand=string.Empty; 
              Size=string.Empty;
          }
      
          public Shirt(string brand, string size)
          {
              Brand=brand;
              Size=size;
          }
      
          public string Brand { get; set; }
          public string Size { get; set; }
      
          public void EditDetails()
          {
              string input;
              Console.Write($"Enter Brand [{Brand}]: ");
              input = Console.ReadLine();
              if (!string.IsNullOrWhiteSpace(input))
              {
                  this.Brand = input;
              }
              Console.Write($"Enter Size [{Size}]: ");
              input = Console.ReadLine();
              if (!string.IsNullOrWhiteSpace(input))
              {
                  this.Size = input;
              }
          }
      
          public override string ToString()
          {
              return $"{Brand}-{Size}";
          }
      }
      
      1. 衬衫包含两个读/写属性,品牌和尺寸。
      2. 新衬衫用空字符串初始化(而不是null
      3. ToString() 方法创建一个人类可读的字符串,表示衬衫的详细信息
      4. EditDetails() 函数一一向用户询问来自Console 的详细信息。它还会显示现有的详细信息(如果有),并且仅在用户输入非空字符串时分配新的详细信息。

      可以在您的程序中使用此类来跟踪商店中的所有衬衫。例如:

      class Program
      {
          static void Main(string[] args)
          {
              bool done = false;
              List<Shirt> store = new List<Shirt>();
              do
              {
                  Console.WriteLine("1. Add New T-shirt Details");
                  Console.WriteLine("2. Edit Existing T-shirt details");
                  Console.WriteLine("3. Display All T-shirts in store");
                  Console.WriteLine("4. Delete T-shirt information");
                  Console.WriteLine("5. Exit");
                  string input = Console.ReadLine();
                  if (int.TryParse(input, out int choice))
                  {
                      switch (choice)
                      {
                          case 1:
                              Shirt shirt = new Shirt();
                              shirt.EditDetails();
                              store.Add(shirt);
                              break;
                          case 2:
                              Shirt last = store[store.Count-1];
                              last.EditDetails();
                              break;
                          case 3:
                              foreach (var item in store)
                              {
                                  Console.WriteLine(item.ToString());
                              }
                              break;
                          case 5:
                              done = true;
                              break;
                          default:
                              break;
                      }
                  }
              } while (!done);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-05
        • 1970-01-01
        • 1970-01-01
        • 2021-12-10
        • 2015-06-08
        • 1970-01-01
        • 2014-01-06
        • 1970-01-01
        相关资源
        最近更新 更多