我怎样才能做到这一点,如果 quantityPurchased 大于或等于 200 它不会超出数组的范围,而不使用 if、if else、else 或 switch 语句。
static double DeterminePercentage(int quantityPurchased)
{
double[] quantity = { 1, 11, 50, 100, 200 };
double[] discount = { 0, 7.5, 15, 17.5, 20 };
int x = 0;
for (int i = 0; i < quantity.Length; i++)
{
if (quantityPurchased >= quantity[i] && quantityPurchased < quantity[i + 1])
{
x = i;
}
break;
}
return discount[x];
}