【发布时间】:2021-01-22 02:31:21
【问题描述】:
为了优化代码,我需要用方法替换 switch 构造,但结果相同。我已经尝试了很长时间,但我做不到。如果您能提供帮助,我将不胜感激!这是我的老师写给我的:“Katrin 在 switch 中,你传递一个值和一个整数。在最简单的情况下可以将其视为数组索引。在更复杂的情况下,可以将其视为通用表达式的元素此值的不同结果。如果您可以编写一个与 switch-case 给出相同结果的函数,那么记录将是紧凑的(好像它由单个 case 块组成),因此不需要 switch-case"
string[] 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;
}
提前谢谢你!
【问题讨论】:
-
我已经修复了您的代码的缩进问题。请确保提供带有适当缩进的代码,因为它使我们更容易阅读。它还使您更容易阅读。
-
不,它们是,因为我复制的 - 它们可能消失了
标签: c# function methods switch-statement