正如其他人所说,您的数组范围太窄(小)。
假设你和 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#代码。