用能给出相同结果的方法替换开关结构。C#
为了优化代码,我需要用一种方法替换开关结构,但结果相同。我已经尝试这么做很长时间了,但我做不到。如果你能帮忙,我将非常感激!以下是我的老师写给我的:“Katrin在开关中,传递一个值和一个整数。在最简单的情况下,可以将其视为数组索引。在更复杂的情况下,作为通用表达式的一个元素,为该值提供不同的结果。如果您可以编写一个函数,该函数的结果与switch case相同,那么记录将是紧凑的(就像它由单个case块组成),因此不需要switch case“用能给出相同结果的方法替换开关结构。C#,c#,function,methods,switch-statement,C#,Function,Methods,Switch Statement,为了优化代码,我需要用一种方法替换开关结构,但结果相同。我已经尝试这么做很长时间了,但我做不到。如果你能帮忙,我将非常感激!以下是我的老师写给我的:“Katrin在开关中,传递一个值和一个整数。在最简单的情况下,可以将其视为数组索引。在更复杂的情况下,作为通用表达式的一个元素,为该值提供不同的结果。如果您可以编写一个函数,该函数的结果与switch case相同,那么记录将是紧凑的(就像它由单个case块组成),因此不需要switch case“ string[]array=newstring[
string[]array=newstring[68]{(68个元素)};//专业的数组
string[]array2=新字符串[44]{(44个元素)};//质量数组:运行状况
string[]array3=新字符串[46]{(46个元素)};//质量数组:字符特征。
string[]array4=新字符串[40]{(40个元素)};//质量数组:一种爱好。
string[]array5=新字符串[41]{(41个元素)};//质量数组:恐惧症。
string[]array6=新字符串[50]{(50个元素)};//行李数组。
string[]array8=新字符串[39]{(39个元素)};//玩家的牌的数组。
string[]array7=新字符串[50]{(50个元素)};//附加信息的数组。
int start=0;
Console.WriteLine(“选择要生成的内容:\n[1]-职业\n[2]-动作卡\n[3]-恐惧症\n[4]-健康状态\n[5]-添加信息\n[6]-行李\n[7]-爱好\n[8]-角色”);
start=Convert.ToInt32(Console.ReadLine());
开关(启动)
{
案例1:
INTP;
int-sycle2;
Console.WriteLine(“我可以生成多少张带有职业的卡片?:”;
p=Convert.ToInt32(Console.ReadLine());
for(sycle2=0;sycle2string[] array = new string[68]{(68 elements)};//The array of professions
string[] array2 = new string[44]{(44 elements)};//Array of qualities: HEALTH status
string[] array3 = new string[46] {(46 elements)};//Array of qualities : Character traits.
string[] array4 = new string[40] {(40 elements)};//Array of qualities : a HOBBY.
string[] array5 = new string[41] {(41 elements)};//Array of qualities : PHOBIAS.
string[] array6 = new string[50] {(50 elements)};//Array of Luggage.
string[] array8 = new string[39] {(39 elements)};//An array of player's cards.
string[] array7 = new string[50] {(50 elements)};//Array of Additional information.
int start = 0;
Console.WriteLine("Choose what to generate: \n[1] - PROFESSION \n[2] - ACTION cards \n[3] - PHOBIAS \n[4] - HEALTH status \n[5] - ADD.Information \n[6] - LUGGAGE \n[7] - HOBBIES \n[8] - CHARACTER ");
start = Convert.ToInt32(Console.ReadLine());
switch (start)
{
case 1:
int p;
int sycle2;
Console.WriteLine("How many cards with Professions can I generate?: ");
p = Convert.ToInt32(Console.ReadLine());
for (sycle2 = 0; sycle2 < p; sycle2++)
{
Console.WriteLine("enter the names of the players to generate cards for: ");
string name = Console.ReadLine();
name += ".txt";
string str1 = sycle2 + 1 + "bynker.txt";
StreamWriter sw2 = File.CreateText(name);
int a7 = rnd.Next(0, 68);
sw2.WriteLine($"Profession : {array[a7]}");
sw2.Close();
}
break;
case 2:
int p1;
int sycle3;
Console.WriteLine("how many cards with additional maps to generate?: ");
p1 = Convert.ToInt32(Console.ReadLine());
for (sycle3 = 0; sycle3 < p1; sycle3++)
{
Console.WriteLine("enter the names of the players to generate cards for: ");
string name = Console.ReadLine();
name += ".txt";
string str2 = sycle3 + 1 + "bynker.txt";
StreamWriter sw3 = File.CreateText(name);
int b3 = rnd.Next(0, 39);
int b4 = rnd.Next(0, 39);
sw3.WriteLine($"action Map: {array8[b3]}");
sw3.WriteLine($"action Map: {array8[b4]}");
sw3.Close();
}
break;
case 3:
int p2;
int sycle4;
Console.WriteLine("How many cards with phobias to generate : ");
p2 = Convert.ToInt32(Console.ReadLine());
for (sycle4 = 0; sycle4 < p2; sycle4++)
{
Console.WriteLine("enter the names of the players to generate cards for: ");
string name = Console.ReadLine();
name += ".txt";
string str1 = sycle4 + 1 + "bynker.txt";
StreamWriter sw4 = File.CreateText(name);
int b = rnd.Next(0, 41);
sw4.WriteLine($"Phobia : {array5[b]}");
sw4.Close();
}
break;
case 4:
int p3;
int sycle5;
Console.WriteLine("How many cards with Diseases(State of Health) to generate?: ");
p3 = Convert.ToInt32(Console.ReadLine());
for (sycle5 = 0; sycle5 < p3; sycle5++)
{
Console.WriteLine("enter the names of the players to generate cards for: ");
string name = Console.ReadLine();
name += ".txt";
string str1 = sycle5 + 1 + "bynker.txt";
StreamWriter sw5 = File.CreateText(name);
int b6 = rnd.Next(5, 80);
int a8 = rnd.Next(0, 44);
sw5.WriteLine($"health status : {array2[a8]} ({b6}%)");
sw5.Close();
}
break;
case 5:
int p4;
int sycle6;
Console.WriteLine("How many cards with Extra.What should I generate?: ");
p4 = Convert.ToInt32(Console.ReadLine());
for (sycle6 = 0; sycle6 < p4; sycle6++)
{
Console.WriteLine("enter the names of the players to generate cards for: ");
string name = Console.ReadLine();
name += ".txt";
string str1 = sycle6 + 1 + "bynker.txt";
StreamWriter sw6 = File.CreateText(name);
int b2 = rnd.Next(0, 50);
sw6.WriteLine($"Add.Information: {array7[b2]}");
sw6.Close();
}
break;
case 6:
int p5;
int sycle7;
Console.WriteLine("How many Baggage cards can I generate?: ");
p5 = Convert.ToInt32(Console.ReadLine());
for (sycle7 = 0; sycle7 < p5; sycle7++)
{
Console.WriteLine("enter the names of the players to generate cards for: ");
string name = Console.ReadLine();
name += ".txt";
string str1 = sycle7 + 1 + "bynker.txt";
StreamWriter sw7 = File.CreateText(name);
int b1 = rnd.Next(0, 50);
sw7.WriteLine($"baggage:{array6[b1]}");
sw7.Close();
}
break;
case 7:
int p6;
int sycle8;
Console.WriteLine("How many Hobby cards can I generate?: ");
p6 = Convert.ToInt32(Console.ReadLine());
for (sycle8 = 0; sycle8 < p6; sycle8++)
{
Console.WriteLine("enter the names of the players to generate cards for: ");
string name = Console.ReadLine();
name += ".txt";
string str1 = sycle8 + 1 + "bynker.txt";
StreamWriter sw8 = File.CreateText(name);
int a10 = rnd.Next(0, 40);
sw8.WriteLine($") : {array4[a10]}");
sw8.Close();
}
break;
case 8:
int p7;
int sycle9;
Console.WriteLine("How many Character cards can I generate?: ");
p7 = Convert.ToInt32(Console.ReadLine());
for (sycle9 = 0; sycle9 < p7; sycle9++)
{
Console.WriteLine("enter the names of the players to generate cards for: ");
string name = Console.ReadLine();
name += ".txt";
string str1 = sycle9 + 1 + "bynker.txt";
StreamWriter sw9 = File.CreateText(name);
int a9 = rnd.Next(0, 46);
sw9.WriteLine($"--Trait : {array3[a9]}");
sw9.Close();
}
break;
}
case 1:
{
int p;
int sycle2;
Console.WriteLine("How many cards with Professions can I generate?: ");
p = Convert.ToInt32(Console.ReadLine());
for (sycle2 = 0; sycle2 < p; sycle2++)
{
Console.WriteLine("enter the names of the players to generate cards for: ");
string name = Console.ReadLine();
name += ".txt";
string str1 = sycle2 + 1 + "bynker.txt";
StreamWriter sw2 = File.CreateText(name);
int a7 = rnd.Next(0, 68);
sw2.WriteLine($"Profession : {array[a7]}");
sw2.Close();
}
break;
}
case 2:
{
int p;
int sycle2;
// Different
Console.WriteLine("how many cards with additional maps to generate?: ");
p = Convert.ToInt32(Console.ReadLine());
for (sycle2 = 0; sycle2 < p; sycle2++)
{
Console.WriteLine("enter the names of the players to generate cards for: ");
string name = Console.ReadLine();
name += ".txt";
string str1 = sycle2 + 1 + "bynker.txt";
StreamWriter sw2 = File.CreateText(name);
// Different
int b3 = rnd.Next(0, 39);
int b4 = rnd.Next(0, 39);
sw2.WriteLine($"action Map: {array8[b3]}");
sw2.WriteLine($"action Map: {array8[b4]}");
// End different
sw2.Close();
}
break;
}
void WriteMethod1(StreamWriter sw)
{
int a7 = rnd.Next(0, 68);
sw2.WriteLine($"Profession : {array[a7]}");
}
void WriteMethod2(StreamWriter sw)
{
int b3 = rnd.Next(0, 39);
int b4 = rnd.Next(0, 39);
sw2.WriteLine($"action Map: {array8[b3]}");
sw2.WriteLine($"action Map: {array8[b4]}");
}
Action<StreamWriter>[] writers = {WriteMethod1,WriteMethod2, ...}
int p;
int sycle2;
Console.WriteLine(messages[start]);
p = Convert.ToInt32(Console.ReadLine());
for (sycle2 = 0; sycle2 < p; sycle2++)
{
Console.WriteLine("enter the names of the players to generate cards for: ");
string name = Console.ReadLine();
name += ".txt";
string str1 = sycle2 + 1 + "bynker.txt";
StreamWriter sw2 = File.CreateText(name);
writers[start](sw2);
sw2.Close();
}