我正在尝试创建一个工作程序,您必须输入城镇,产品和数量并输出总价格。
例如,Town1> Milk> 2应该生成2
。但由于某种原因,没有产出。有人可以帮助我并告诉我错误吗?
以下是代码:
Console.Write("Enter product: ");
var product = Console.ReadLine().ToLower();
Console.Write("Enter town: ");
var town = Console.ReadLine().ToLower();
Console.Write("Enter quantity: ");
var quantity = double.Parse(Console.ReadLine());
if (town == "Town1")
{
if (product == "Milk")
Console.WriteLine(1.50 * quantity);
if (product == "Water")
Console.WriteLine(0.80 * quantity);
if (product == "Whiskey")
Console.WriteLine(4.20 * quantity);
if (product == "Peanuts")
Console.WriteLine(0.90 * quantity);
if (product == "Chocolate")
Console.WriteLine(2.60 * quantity);
}
if (town == "Town2")
{
if (product == "Milk")
Console.WriteLine(1.40 * quantity);
if (product == "Water")
Console.WriteLine(0.70 * quantity);
if (product == "Whiskey")
Console.WriteLine(3.90 * quantity);
if (product == "Peanuts")
Console.WriteLine(0.70 * quantity);
if (product == "Chocolate")
Console.WriteLine(1.50 * quantity);
}
if (town == "Town3")
{
if (product == "Milk")
Console.WriteLine(1.90 * quantity);
if (product == "Water")
Console.WriteLine(1.50 * quantity);
if (product == "Whiskey")
Console.WriteLine(5.10 * quantity);
if (product == "Peanuts")
Console.WriteLine(1.35 * quantity);
if (product == "Chocolate")
Console.WriteLine(3.10 * quantity);
}
}}}
答案 0 :(得分:7)
您正在设置town = value.ToLower()
和product = value.ToLower()
,这会使所有字符都小写,请更改这些行:
var town = Console.ReadLine().ToLower();
var product = Console.ReadLine().ToLower();
对此:
var town = Console.ReadLine();
var product = Console.ReadLine();
或者更改if语句条件以使用小写值作为比较
if (town == "town1")
{
等...
答案 1 :(得分:1)
对于奖励积分,请考虑使用Dictionary<,>
,这也允许您指定应执行不区分大小写的比较。
var townProductPrices = new Dictionary<string, Dictionary<string, double>>(StringComparer.CurrentCultureIgnoreCase) {
["Town1"] = new Dictionary<string, double>(StringComparer.CurrentCultureIgnoreCase) {
["Milk"] = 1.50d,
["Water"] = 0.80d,
["Whiskey"] = 4.20d,
["Peanuts"] = 0.90d,
["Chocolate"] = 2.60d,
},
["Town2"] = new Dictionary<string, double>(StringComparer.CurrentCultureIgnoreCase) {
["Milk"] = 1.40d,
["Water"] = 0.70d,
["Whiskey"] = 3.90d,
["Peanuts"] = 0.70d,
["Chocolate"] = 1.50d,
},
//...
};
Console.Write("Enter product: ");
var product = Console.ReadLine().Trim();
Console.Write("Enter town: ");
var town = Console.ReadLine().Trim();
Console.Write("Enter quantity: ");
var quantity = double.Parse(Console.ReadLine().Trim());
var productPrices = townProductPrices[town];
var price = productPrices[product];
var total = price * quantity;
Console.WriteLine(total.ToString("c"));
答案 2 :(得分:-1)
你不需要这么多比赛。如果最重要的话,试试这个:
public class Program
{
private static void Main(string[] args)
{
Console.Write("Enter product: ");
var product = Console.ReadLine().ToLower();
Console.Write("Enter town: ");
var town = Console.ReadLine().ToLower();
Console.Write("Enter quantity: ");
var quantity = double.Parse(Console.ReadLine());
var mapperData = new List<Mapper>()
{
new Mapper { TownName = "Town1", ProductInfo = "Milk", Quantity = 1.50 },
new Mapper { TownName = "Town1", ProductInfo = "Water", Quantity = 0.80 },
new Mapper { TownName = "Town1", ProductInfo = "Whiskey", Quantity = 4.20 },
new Mapper { TownName = "Town1", ProductInfo = "Peanuts", Quantity = 0.90 },
new Mapper { TownName = "Town1", ProductInfo = "Chocolate", Quantity = 2.60 },
new Mapper { TownName = "Town2", ProductInfo = "Milk", Quantity = 1.40 },
new Mapper { TownName = "Town2", ProductInfo = "Water", Quantity = 0.70 },
new Mapper { TownName = "Town2", ProductInfo = "Whiskey", Quantity = 3.90 },
new Mapper { TownName = "Town2", ProductInfo = "Peanuts", Quantity = 0.70 },
new Mapper { TownName = "Town2", ProductInfo = "Chocolate", Quantity = 1.50 },
new Mapper { TownName = "Town3", ProductInfo = "Milk", Quantity = 1.90 },
new Mapper { TownName = "Town3", ProductInfo = "Water", Quantity = 1.50 },
new Mapper { TownName = "Town3", ProductInfo = "Whiskey", Quantity = 5.10 },
new Mapper { TownName = "Town3", ProductInfo = "Peanuts", Quantity = 1.35 },
new Mapper { TownName = "Town3", ProductInfo = "Chocolate", Quantity = 3.10 },
};
var matchingQuantity = mapperData.FirstOrDefault(i => i.TownName.ToString().ToLower() == town.ToLower().Trim()
&& i.ProductInfo.ToString().ToLower() == product.ToLower().Trim()).Quantity;
Console.WriteLine(matchingQuantity * quantity);
}
}
public class Mapper
{
public string TownName { get; set; }
public string ProductInfo { get; set; }
public double Quantity { get; set; }
}
此外,您甚至可以为城镇和产品创建枚举并使用它们。