我正在尝试创建一个工作程序,您必须在其中输入城镇,产品和数量并输出总价。
例如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);
}
}}}
您正在设置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")
{
等等...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句