我正在尝试创建一个工作程序,您必须在其中输入城镇、产品和数量并输出总价。
例如 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")
{
等...
关于c# - 为什么这段代码不打印任何东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49429682/