C# 关于 for loop/switch 语句的适当性。如何在 switch 语句中使用 arrayList?
C# Regarding for loop/ switch statement appropriateness. How to use an arrayList in switch statement?
第一个问题是关于将其保留为 IF 语句还是将其更改为 switch 语句是否更好的编程实践。
public Booking bookVehicle(String vehicleClass, Client potentialClient, int number)
{
ArrayList vehicles;
if (vehicleClass.Equals("a", StringComparison.OrdinalIgnoreCase))
vehicles = thisLocation.getVehicleClass("Prestige Sedan");
else if (vehicleClass.Equals("b", StringComparison.OrdinalIgnoreCase))
vehicles = thisLocation.getVehicleClass("Luxury Sedan");
else if (vehicleClass.Equals("c", StringComparison.OrdinalIgnoreCase))
vehicles = thisLocation.getVehicleClass("Full-size Sedan");
else if (vehicleClass.Equals("d", StringComparison.OrdinalIgnoreCase))
vehicles = thisLocation.getVehicleClass("Mid-size Sedan");
else if (vehicleClass.Equals("e", StringComparison.OrdinalIgnoreCase))
vehicles = thisLocation.getVehicleClass("Hatchback");
else if (vehicleClass.Equals("f", StringComparison.OrdinalIgnoreCase))
vehicles = thisLocation.getVehicleClass("Compact");
else vehicles = thisLocation.getVehicleClass("Compact");
if (available(vehicles, number))
{
ArrayList hiredVehicles = new ArrayList();
int currentVehicle = 0;
int hireCharge = 0;
while (number > 0)
{
Vehicle thisVehicle = (Vehicle)vehicles[currentVehicle];
if (!thisVehicle.hired)
{
hiredVehicles.Add(thisVehicle);
hireCharge += thisVehicle.hireFee;
number--;
}
currentVehicle++;
}
return new Booking(potentialClient, hireCharge, hiredVehicles);
}
else
{
return new Booking(potentialClient, false);
}
}
所以上面的代码正在评估车辆的 ArrayList,车辆的属性之一是 "Vehicle Class" 通过使用 IF 语句。使用 Switch 语句 why/why 不是更好吗??
下面是我的代码,用于尝试实现与上面相同的功能,但使用 switch 语句,将其放入一个单独的方法中,该方法被调用,但我不知道如何使用 arrayList。
switch (ArrayList vehicles)
{
case 'a':
vehicles = thislocation.getVehicleClass("prestige Sedan");
break;
case 'b':
vehicles = thisLocation.getVehicleClass("Luxury Sedan");
break;
case 'c':
vehicles = thisLocation.getVehicleClass("Full-size Sedan");
break;
case 'd':
vehicles = thisLocation.getVehicleClass("Mid-size Sedan");
break;
case 'e':
vehicles = thisLocation.getVehicleClass("Hatchback");
break;
case 'f':
vehicles = thisLocation.getVehicleClass("Compact");
break;
default:
vehicles = thisLocation.getVehicleClass("Compact");
break;
return vehicles;
}
如果你能帮助我使这个 switch 语句与 if 语句(if possible/appropriate)具有相同的功能,我将不胜感激。
switch
语句指定要测试其值的变量。
您正在测试字符串的值,而不是 ArrayList:
switch (String vehicleClass)
至于 switch
块优于 if/else
块的适当性,这是一个见仁见智的问题。使用您更容易阅读和维护的那个。
有一种方法可以完全避免 switch/if。有一个字典,其中包含字符串 a、b、c、d、e 的键和 ArrayList 的条目 return 所需车辆的列表 class,类似以下内容:
Dictionary<string, ArrayList> vehicleDict = {
{"a", thisLocation.getVehicleClass("whatever"},
{...},
{"e", thisLocation.getVehicleClass("some other class")}
};
然后您的方法的初始部分变为 ArrayList vehicles = vehicleDict[vehicleClass];
注意 1:使用枚举而不是字符串会更好
注意 2:如果这不能编译,可能是因为我在 phone 上打字。
注3:我从来没有打过大小写;我相信你能做到。当然,将字典放在有意义的地方取决于您。
第一个问题是关于将其保留为 IF 语句还是将其更改为 switch 语句是否更好的编程实践。
public Booking bookVehicle(String vehicleClass, Client potentialClient, int number)
{
ArrayList vehicles;
if (vehicleClass.Equals("a", StringComparison.OrdinalIgnoreCase))
vehicles = thisLocation.getVehicleClass("Prestige Sedan");
else if (vehicleClass.Equals("b", StringComparison.OrdinalIgnoreCase))
vehicles = thisLocation.getVehicleClass("Luxury Sedan");
else if (vehicleClass.Equals("c", StringComparison.OrdinalIgnoreCase))
vehicles = thisLocation.getVehicleClass("Full-size Sedan");
else if (vehicleClass.Equals("d", StringComparison.OrdinalIgnoreCase))
vehicles = thisLocation.getVehicleClass("Mid-size Sedan");
else if (vehicleClass.Equals("e", StringComparison.OrdinalIgnoreCase))
vehicles = thisLocation.getVehicleClass("Hatchback");
else if (vehicleClass.Equals("f", StringComparison.OrdinalIgnoreCase))
vehicles = thisLocation.getVehicleClass("Compact");
else vehicles = thisLocation.getVehicleClass("Compact");
if (available(vehicles, number))
{
ArrayList hiredVehicles = new ArrayList();
int currentVehicle = 0;
int hireCharge = 0;
while (number > 0)
{
Vehicle thisVehicle = (Vehicle)vehicles[currentVehicle];
if (!thisVehicle.hired)
{
hiredVehicles.Add(thisVehicle);
hireCharge += thisVehicle.hireFee;
number--;
}
currentVehicle++;
}
return new Booking(potentialClient, hireCharge, hiredVehicles);
}
else
{
return new Booking(potentialClient, false);
}
}
所以上面的代码正在评估车辆的 ArrayList,车辆的属性之一是 "Vehicle Class" 通过使用 IF 语句。使用 Switch 语句 why/why 不是更好吗??
下面是我的代码,用于尝试实现与上面相同的功能,但使用 switch 语句,将其放入一个单独的方法中,该方法被调用,但我不知道如何使用 arrayList。
switch (ArrayList vehicles)
{
case 'a':
vehicles = thislocation.getVehicleClass("prestige Sedan");
break;
case 'b':
vehicles = thisLocation.getVehicleClass("Luxury Sedan");
break;
case 'c':
vehicles = thisLocation.getVehicleClass("Full-size Sedan");
break;
case 'd':
vehicles = thisLocation.getVehicleClass("Mid-size Sedan");
break;
case 'e':
vehicles = thisLocation.getVehicleClass("Hatchback");
break;
case 'f':
vehicles = thisLocation.getVehicleClass("Compact");
break;
default:
vehicles = thisLocation.getVehicleClass("Compact");
break;
return vehicles;
}
如果你能帮助我使这个 switch 语句与 if 语句(if possible/appropriate)具有相同的功能,我将不胜感激。
switch
语句指定要测试其值的变量。
您正在测试字符串的值,而不是 ArrayList:
switch (String vehicleClass)
至于 switch
块优于 if/else
块的适当性,这是一个见仁见智的问题。使用您更容易阅读和维护的那个。
有一种方法可以完全避免 switch/if。有一个字典,其中包含字符串 a、b、c、d、e 的键和 ArrayList 的条目 return 所需车辆的列表 class,类似以下内容:
Dictionary<string, ArrayList> vehicleDict = {
{"a", thisLocation.getVehicleClass("whatever"},
{...},
{"e", thisLocation.getVehicleClass("some other class")}
};
然后您的方法的初始部分变为 ArrayList vehicles = vehicleDict[vehicleClass];
注意 1:使用枚举而不是字符串会更好
注意 2:如果这不能编译,可能是因为我在 phone 上打字。
注3:我从来没有打过大小写;我相信你能做到。当然,将字典放在有意义的地方取决于您。