簡體   English   中英

(C#) 我怎樣才能使所有這些“else if”語句更簡潔,我怎樣才能更好地優化我的代碼?

[英](C#) How can I make all these "else if" statements more condensed and how can I optimize my code better?

好的,我有一大堆else if語句,有沒有辦法將它們壓縮成更少的代碼行? 喜歡為所有這些做一個 if 語句嗎? 有什么方法可以使我的代碼更優化和更易於閱讀?

            int x;
            int y;
            int time=0;
            Random rng = new Random();
            int hrand_num = rng.Next(-24000, 24000);
            int vrand_num = rng.Next(-24000, 24000);
            x = hrand_num;
            y = vrand_num;
            


            while (true)
            {
                ConsoleKey key = Console.ReadKey().Key;
                
                 
                if (key == ConsoleKey.UpArrow)
                {


                    y=y+2000;
                   
                }
                else if (key == ConsoleKey.DownArrow)

                {

                   y=y-2000;

                }
                else if (key == ConsoleKey.LeftArrow)
                {
                    x = x-1000;
                }
                else if (key == ConsoleKey.RightArrow)
                {
                    x = x+1000;
                }
                // Circumnavigate Players Position.
                  // North and South
                if (y >= 24001)
                {
                    y = -24000;
                }

                else if (y <= -24001)
                {
                    y = 24000;
                }
                  //West and East
                else if (x >= 24001)
                {
                    x = -24000;
                }
                else if (x <= -24001)
                {
                    x = 24000;
                }

                // Setting Time Zones

                if (x >= -2000 && x <= 0 )
                {
                    time = 0;
                }
                else if (x >=
                    1 && x <= 2000)
                {
                    time = 1;
                }
                else if (x >= 2001 && x <=4000)
                {
                    time = 2;
                }

                else if (x >= 4001 && x <= 6000)
                {
                    time = 3;
                }

                else if (x >= 6001 && x <= 8000)
                {
                    time = 4;
                }

                else if (x >= 8001 && x <= 10000)
                {
                    time = 5;
                }
                else if (x >= 10001 && x <= 12000)
                {
                    time = 6;
                }
                else if (x >= 12001 && x <= 14000)
                {
                    time =  7;
                }
                else if (x >= 14001 && x <= 16000)
                {
                    time =  8;
                }
                else if (x >= 16001 && x <= 18000)
                {
                    time =  9;
                }
                else if (x >= 18001 && x <= 20000)
                {
                    time =  10;
                }
                else if (x >= 20001 && x <= 22000)
                {
                    time =  11;
                }
                else if (x >= 22001 && x <= 24000)
                {
                    time = 12;
                }
                else if (x == -24000 && x <= -22001)
                {
                    time = 13;
                }
                else if (x >= -22000 && x <= -20001 )
                {
                    time = 14;
                }
                else if (x >= -20000 && x <= -18001)
                {
                    time = 15;
                }
                else if (x >= -18000 && x <= -16001)
                {
                    time = 16;
                }
                else if (x >= -16000 && x <= -14001)
                {
                    time =  17;
                }
                else if (x >= -14000 && x <= -12001)
                {
                    time = 18;
                }
                else if (x >= -12000 && x <= -10001)
                {
                    time = 19;
                }
                else if (x >= -10000 && x <= -8001)
                {
                    time =  20;
                }
                else if (x >= -8000 && x <= -6001)
                {
                    time = 21;
                }
                else if (x >= -6000 && x <= -4001)
                {
                    time = 22;
                }
                else if (x >= -4000 && x <= -2001)
                {
                    time = 23;
                }
 
                Console.WriteLine($"X: {x,6} Y: {y,6} Time: {time,3}");
            }   
         ```     

假設您使用的是 C# 8.0,您可以查看 switch 語句和 switch 表達式: https://learn.microsoft.com/en-us/do.net/csharp/language-reference/operators/switch-expression (此外,模式文檔也很有幫助: https://learn.microsoft.com/en-us/do.net/csharp/language-reference/operators/patterns

所以你可以這樣寫:

switch (key)
{
  case ConsoleKey.UpArrow:
    y=y+2000;
    break;
  // [...]
}

time = x switch
{
  >= -2000 and <= 0 => 0,
  >= 1 and <= 2000 => 1
  // [...]
};

我建議您利用:

  • 用於解釋箭頭鍵的switch 語句
    • 將 switch 語句與 if 循環進行比較,您可以將第一個case視為if條件,將 remaning case s 視為else if條件
  • Math.Abs( )Math.Sign( )用於環繞玩家的 position
    • Math.Abs( )返回變量的絕對值; 例如,對於x = 5x = -5Math.Abs(x)都會返回5
    • Math.Sign( )返回值的符號; 如果值為負數,則返回-1 如果是正數,則返回1 如果兩者都不是 (0),則返回0 這有助於我們確定更新值的所需符號。
  • 用於設置time開關表達式
    • 鑒於time值最終由x確定,您可以使用 switch 表達式而不是 switch 語句來確定其值。 switch 表達式表示您想根據x的值確定time的值; 並將以下每個條件與x進行比較( <= -22001計算為x <= -22001 )。 如果條件評估為true ,則提供的值設置為time值( => 13然后設置time = 13 )。

它可以這樣實現:

int x;
int y;
Random rng = new Random();
int hrand_num = rng.Next(-24000, 24000);
int vrand_num = rng.Next(-24000, 24000);
x = hrand_num;
y = vrand_num;

while (true)
{
    switch (Console.ReadKey().Key)
    {
        case ConsoleKey.UpArrow:
            y += 2000;
            break;
        case ConsoleKey.DownArrow:
            y -= 2000;
            break;
        case ConsoleKey.LeftArrow:
            x -= 1000;
            break;
        case ConsoleKey.RightArrow:
            x += 1000;
            break;
    }

    // Circumnavigate Players Position.
    // North and South
    if (Math.Abs(y) > 24000)
    {
        y = -(Math.Sign(y) * 24000);
    }
    //West and East
    else if (Math.Abs(x) > 24000)
    {
        x = -(Math.Sign(x) * 24000);
    }

    // Setting Time Zones
    var time = x switch
    {
        <= -22001   => 13,
        <= -20001   => 14,
        <= -18001   => 15,
        <= -16001   => 16,
        <= -14001   => 17,
        <= -12001   => 18,
        <= -10001   => 19,
        <= -8001    => 20,
        <= -6001    => 21,
        <= -4001    => 22,
        <= -2001    => 23,
        <= 0        => 0,
        <= 2000     => 1,
        <= 4000     => 2,
        <= 6000     => 3,
        <= 8000     => 4,
        <= 10000    => 5,
        <= 12000    => 6,
        <= 14000    => 7,
        <= 16000    => 8,
        <= 18000    => 9,
        <= 20000    => 10,
        <= 22000    => 11,
        <= 24000    => 12,
        _ => 0
    };

    Console.WriteLine($"X: {x,6} Y: {y,6} Time: {time,3}");

我還建議引入一些常量; 特別是對於價值24000

您可以使用以下內容涵蓋所有time案例

var time = x <= 24000
    ? x / 2000 + 1;
    : (24000 - x) / 2000 + 13;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM