我实际上对编程很新,也许我只是累了,但我在这里找不到问题。 result
似乎没有改变它的价值。如您所见,第一个输入为product
,第二个输入为city
,最后是我们想要的产品的amount
。该程序必须打印最终价格(result
)。
static void Main(string[] args)
{
string product = Console.ReadLine().ToLower();
string city = Console.ReadLine().ToLower();
double amount = double.Parse(Console.ReadLine());
double result;
if (city == "Sofia")
{
if (product == "coffee")
{
result = amount * 0.50;
Console.WriteLine(result);
}
else if (product == "water")
{
result = amount * 0.80;
Console.WriteLine(result);
}
else if (product == "beer")
{
result = amount * 1.20;
Console.WriteLine(result);
}
else if (product == "sweets")
{
result = amount * 1.45;
Console.WriteLine(result);
}
else if (product == "peanuts")
{
result = amount * 1.60;
Console.WriteLine(result);
}
}
else if (city == "Plovdiv")
{
if (product == "coffee")
{
result = amount * 0.40;
Console.WriteLine(result);
}
else if (product == "water")
{
result = amount * 0.70;
Console.WriteLine(result);
}
else if (product == "beer")
{
result = amount * 1.15;
Console.WriteLine(result);
}
else if (product == "sweets")
{
result = amount * 1.30;
Console.WriteLine(result);
}
else if (product == "peanuts")
{
result = amount * 1.50;
Console.WriteLine(result);
}
}
else if (city == "Varna")
{
if (product == "coffee")
{
result = amount * 0.45;
Console.WriteLine(result);
}
else if (product == "water")
{
result = amount * 0.70;
Console.WriteLine(result);
}
else if (product == "beer")
{
result = amount * 1.10;
Console.WriteLine(result);
}
else if (product == "sweets")
{
result = amount * 1.35;
Console.WriteLine(result);
}
else if (product == "peanuts")
{
result = amount * 1.55;
Console.WriteLine(result);
}
else
{
Console.WriteLine("Invalid");
}
}
答案 0 :(得分:5)
您将city
转换为小写,然后使用大写首字母进行比较!这不会很好。
即。你需要
if (city == "sofia")
等
您可以使用逐行调试器来验证这一点。花一些时间学习如何使用它。调试比能够输入代码更重要。
答案 1 :(得分:1)
你能看到代码的精巧程度吗?让我们将实现拆分为模型和业务逻辑(C#6.0):
// Dictionary of dictionary; a better solution is to implement a custom class for it
private static Dictionary<string, Dictionary<string, double>> s_Model =
new Dictionary<string, Dictionary<string, double>>(StringComparer.OrdinalIgnoreCase) {
{ "Sofia", new Dictionary<string, double>(StringComparer.OrdinalIgnoreCase) {
{ "coffee", 0.80},
{ "beer", 1.20},
{ "water", 0.50} } },
{ "Plovdiv", new Dictionary<string, double>(StringComparer.OrdinalIgnoreCase) {
{ "coffee", 0.70},
{ "water", 0.45} } },
};
不仅仅是使用模型;尝试获取所提供的products
的{{1}};然后为所需的city
寻找price
:
product
答案 2 :(得分:0)
正如Bathsheba所说,你需要将比较改为小写,因为比较是准确的。另外,我建议在最后加Console.ReadKey();
,以便在得到结果之前不要关闭。
示例:
Console.Write("Product: ");
string product = Console.ReadLine().ToLower();
Console.Write("City: ");
string city = Console.ReadLine().ToLower();
Console.Write("Price: ");
double amount = Convert.ToDouble(Console.ReadLine());
double result;
if (city == "sofia")
{
if (product == "coffee")
{
result = amount * 0.50;
Console.WriteLine(result);
}
}
Console.ReadKey();
}
}
我在运行时将Console.Write();
放在那里以便于阅读,所以我可以测试它。
对于许多if
,我建议使用switch语句。
例如。
switch(city)
{
case "sofia":
switch (product)
{
case "coffee":
result = amount * 0.50;
Console.WriteLine(result);
break;
}
break;
case "plovdiv":
switch (product)
{
case "coffee":
result = amount * 0.40;
Console.WriteLine(result);
break;
}
break;
}
答案 3 :(得分:0)
有人已经看到了小写问题。而且作为你拥有的筑巢的替代品。为了清洁代码,请更改为switch语句以减少嵌套并提高代码的可读性。
var product = "coffee";
var city = "VarNa";
var amount = 10.00;
double result;
switch (city.ToLower())
{
case "sofia":
switch (product)
{
case "coffee":
result = amount * 0.50;
Console.WriteLine(result);
break;
case "water":
result = amount * 0.80;
Console.WriteLine(result);
break;
case "beer":
result = amount * 1.20;
Console.WriteLine(result);
break;
case "sweets":
result = amount * 1.45;
Console.WriteLine(result);
break;
case "peanuts":
result = amount * 1.60;
Console.WriteLine(result);
break;
}
break;
case "plovdiv":
switch (product)
{
case "coffee":
result = amount * 0.40;
Console.WriteLine(result);
break;
case "water":
result = amount * 0.70;
Console.WriteLine(result);
break;
case "beer":
result = amount * 1.15;
Console.WriteLine(result);
break;
case "sweets":
result = amount * 1.30;
Console.WriteLine(result);
break;
case "peanuts":
result = amount * 1.50;
Console.WriteLine(result);
break;
}
break;
case "varna":
switch (product)
{
case "coffee":
result = amount * 0.45;
Console.WriteLine(result);
break;
case "water":
result = amount * 0.70;
Console.WriteLine(result);
break;
case "beer":
result = amount * 1.10;
Console.WriteLine(result);
break;
case "sweets":
result = amount * 1.35;
Console.WriteLine(result);
break;
case "peanuts":
result = amount * 1.55;
Console.WriteLine(result);
break;
default:
Console.WriteLine("Invalid");
break;
}
break;
}