1

我是数组的新手,但必须有比我正在做的更好的循环数组的方法。这段代码很难看。请让我知道任何想法。这个程序的想法是,当用户输入他们需要的衬衫尺寸时,他们将根据数量获得折扣。如果超过 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);
4

1 回答 1

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) 查找。

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

于 2013-11-04T21:37:25.713 回答