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