C# 将用户信息存储到阵列以用于交换机状态

C# 将用户信息存储到阵列以用于交换机状态,c#,arrays,.net,C#,Arrays,.net,我的任务是菜单中的选项,目前我一直在尝试将用户输入(在本例中是带有t恤品牌和大小的字符串)存储到一个数组中,以便稍后在其他一些菜单选项中使用 namespace A4pf { class Program { static void Main(string[] args) { int option = 0; Console.WriteLine("1. Add New T-shirt Deta

我的任务是菜单中的选项,目前我一直在尝试将用户输入(在本例中是带有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();
        } 
     
    }
}
名称空间A4pf
{
班级计划
{
静态void Main(字符串[]参数)
{
int选项=0;
Console.WriteLine(“1.添加新T恤细节”);
Console.WriteLine(“2.编辑现有T恤细节”);
Console.WriteLine(“3.展示店内所有T恤”);
Console.WriteLine(“4.删除T恤信息”);
控制台写入线(“5.退出”);
开关(选件)
{
案例1:
Console.WriteLine(“请输入T恤衫详细信息”);
控制台。书写(“品牌名称和尺寸(如Thrasher-M”);
字符串[]tshirtDetails=新字符串[12];
for(int i=0;i
您正在开关案例1中创建一个数组,因此每次输入案例1时,都会创建新的数组,从而删除以前的数据。我建议您尝试将该行移到
int option=0
上方,以便数组中的当前数据在其他案例中也可用,或者至少移动如果您想在输入案例1时删除以前的数据,请在开关案例外部设置数组。

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

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

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

string[] tshirtDetails = new string[1];
下面是一个伪代码示例:

修订版#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
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)
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) 
}
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)
}
现在我们已经对需要发生的事情有了一个整体的想法,我们可以为不太清楚的部分添加更多的细节

修订版#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 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
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)
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) 
}
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)
}
查看了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 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
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)
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) 
}
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)
}
“在数组中存储t恤细节”。我的数组大小为1,如果用户输入三件t恤的细节会发生什么情况?在将每个t恤细节添加到数组中之前,我最好调整数组的大小。让我们在“在数组中存储t恤细节(tshirtDetails)”之前为选项#1添加一些伪代码

我们继续查看伪代码:

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 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
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)
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) 
}
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。我已将其注释掉。由于“Case 5”及其包含的代码不是必需的,因此可以删除(删除)

注释
//ToDo:addcode
表示您可以