循环遍历数组 - 更好的方法?

时间:2013-11-04 21:34:45

标签: c# arrays

我是数组的新手,但是必须有一种比我正在做的更好的循环数组的方法。这段代码很难看。请让我知道任何想法。该计划的想法是当用户输入他们需要的尺码时,他们将根据数量获得折扣。如果超过2(他们获得10%的折扣,如果> = 3和< = 4他们获得15%的折扣,如果> = 5他们将获得25%的折扣。

这是我的代码:

string userInputString;
string userInput;
int userInputNo= 0;
double userPrice = 0;
string userSize = "";
string[,] prices = {{"S", "5.00"}, {"M", "7.00"}, {"L", "9.00"}, {"X", "12.00"} };

Console.Write("What size shirt (S, M, L, OR X-Large): ");

userInputString = Console.ReadLine();
userInput = Convert.ToString(userInputString);
Console.Write("How many shirts do you need?: ");
userInputString = Console.ReadLine();
userInputNo = Convert.ToInt32(userInputString);

if (userInputNo == 2)
{
    if (userInput == prices[0,0])
    {
        userPrice = ((Convert.ToDouble(prices[0,1]) * 0.10) +  Convert.ToDouble(prices[0,1]));
        userSize = prices[0,0].ToString();
    }
    else if (userInput == prices[1, 0])
    {
        userPrice = ((Convert.ToDouble(prices[1, 1]) * 0.10) + Convert.ToDouble(prices[1, 1]));
        userSize = prices[1, 0].ToString();
    }
    else if (userInput == prices[2, 0])
    {
        userPrice = ((Convert.ToDouble(prices[2, 1]) * 0.10) + Convert.ToDouble(prices[2, 1]));
        userSize = prices[2, 0].ToString();
    }
    else
    {
        userPrice = ((Convert.ToDouble(prices[3, 1]) * 0.10) + Convert.ToDouble(prices[3, 1]));
        userSize = prices[3, 0].ToString();
    }
}
else if (userInputNo >= 3 && userInputNo <= 4)
{
    if (userInput == prices[0, 0])
    {
        userPrice = ((Convert.ToDouble(prices[0, 1]) * 0.15) + Convert.ToDouble(prices[0, 1]));
        userSize = prices[0, 0].ToString();
    }
    else if (userInput == prices[1, 0])
    {
        userPrice = ((Convert.ToDouble(prices[1, 1]) * 0.15) + Convert.ToDouble(prices[1, 1]));
        userSize = prices[1, 0].ToString();
    }
    else if (userInput == prices[2, 0])
    {
        userPrice = ((Convert.ToDouble(prices[2, 1]) * 0.15) + Convert.ToDouble(prices[2, 1]));
        userSize = prices[2, 0].ToString();
    }
    else
    {
        userPrice = ((Convert.ToDouble(prices[3, 1]) * 0.15) + Convert.ToDouble(prices[3, 1]));
        userSize = prices[3, 0].ToString();
    }
}
else if (userInputNo >= 5)
{
    if (userInput == prices[0, 0])
    {
        userPrice = ((Convert.ToDouble(prices[0, 1]) * 0.20) + Convert.ToDouble(prices[0, 1]));
        userSize = prices[0, 0].ToString();
    }
    else if (userInput == prices[1, 0])
    {
        userPrice = ((Convert.ToDouble(prices[1, 1]) * 0.20) + Convert.ToDouble(prices[1, 1]));
        userSize = prices[1, 0].ToString();
    }
    else if (userInput == prices[2, 0])
    {
        userPrice = ((Convert.ToDouble(prices[2, 1]) * 0.20) + Convert.ToDouble(prices[2, 1]));
        userSize = prices[2, 0].ToString();
    }
    else
    {
        userPrice = ((Convert.ToDouble(prices[3, 1]) * 0.20) + Convert.ToDouble(prices[3, 1]));
        userSize = prices[3, 0].ToString();
    }
}
else
{
    if (userInput == prices[0, 0])
    {
        userPrice = Convert.ToDouble(prices[0,1]);
        userSize = prices[0, 0].ToString();
    }
    else if (userInput == prices[1, 0])
    {
        userPrice = Convert.ToDouble(prices[1,1]);
        userSize = prices[1, 0].ToString();
    }
    else if (userInput == prices[2, 0])
    {
        userPrice = Convert.ToDouble(prices[2,1]);
        userSize = prices[2, 0].ToString();
    }
    else
    {
        userPrice = Convert.ToDouble(prices[3,1]);
        userSize = prices[3, 0].ToString();
    }
}

Console.WriteLine("For a size {0}, you will pay $ {1}.", userSize.ToString(), userPrice);

1 个答案:

答案 0 :(得分:3)

您的代码中没有循环。但是,您需要查找表而不是数组,因此请使用Dictionary<K,V>

var dict = new Dictionary<string, double> {
    {"S", 0.05},
    {"M", 0.07}, 
    {"L", 0.09}, 
    {"X", 0.12}
};

然后......

userInputString = Console.ReadLine();
var discount = 0.0;
if (dict.TryGetValue(userInputString, out discount))
{
    // discount now holds the discount amount.
    // multiply to get the effective price
}
else
{
    // bad input, alert user
}

// side note: TryGetValue is a method which allows you to
// both test if a key exists and get the value at the same
// time.  if you know that a dictionary contains the key
// you need you can just index into it, i.e.,
// var discount = dict[userInputString];

如果你被家庭作业指令强迫使用数组,那么循环将如下所示:

var discount = 0.0;
for(int i = 0; i < prices.GetLength(0); ++i)
{
    if (String.Equals(prices[i, 0], userInputString, StringComparison.OrdinalIgnoreCase))
    {
        discount = prices[i, 0];            
        break;  // found it, all done
    }
}

然而,这是错误的路线。这里不需要O(n)查找。

此外,不要将所有内容存储为字符串,只是为了将其转换回数字。你正在向后编写代码;在编写代码和然后格式的输出时考虑数据。

相关问题