(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://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression (additionaly the patterns documentation is also helpful: https://docs.microsoft.com/en-us/dotnet/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
// [...]
};
我建议您利用:
- a switch statement 用于解释方向键
- 将 switch 语句与 if 循环进行比较,您可以将第一个
case
视为 if
条件,将后面的 case
视为 else if
条件
Math.Abs( )
and Math.Sign( )
用于环绕玩家的位置
Math.Abs( )
returns 变量的绝对值;例如Math.Abs(x)
return 5
x = 5
和 x = -5
Math.Sign( )
returns 值的符号;如果值为负数,则returns -1
;如果是正数,则 returns 1
;如果两者都不是 (0),则它 returns 0
。这有助于我们确定更新值的所需符号。
- a switch expression 用于设置
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;
好的,我有一大堆 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://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression (additionaly the patterns documentation is also helpful: https://docs.microsoft.com/en-us/dotnet/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
// [...]
};
我建议您利用:
- a switch statement 用于解释方向键
- 将 switch 语句与 if 循环进行比较,您可以将第一个
case
视为if
条件,将后面的case
视为else if
条件
- 将 switch 语句与 if 循环进行比较,您可以将第一个
Math.Abs( )
andMath.Sign( )
用于环绕玩家的位置Math.Abs( )
returns 变量的绝对值;例如Math.Abs(x)
return5
x = 5
和x = -5
Math.Sign( )
returns 值的符号;如果值为负数,则returns-1
;如果是正数,则 returns1
;如果两者都不是 (0),则它 returns0
。这有助于我们确定更新值的所需符号。
- a switch expression 用于设置
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;