【问题标题】:Replace the Switch construction with a method that gives the same results.C#用给出相同结果的方法替换 Switch 构造。C#
【发布时间】: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


【解决方案1】:

我不想为你解决整个程序,但这里只是看前两个案例的过程。

  1. 将子句用大括号括起来,这意味着您可以重复使用变量名。
  2. 使变量名一致
  3. 标记不同的行。

这给出了:

  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;
  }

第一个区别只是消息的文本,所以这可以通过strings 的数组来解决start

第二个区别更复杂。这可以通过多种方式解决。一种是模板方法设计模式,但我会使用一系列操作:

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();
  }

对任何错误表示歉意。重要的是过程,而不是细节。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 2022-06-11
    相关资源
    最近更新 更多