gpt4 book ai didi

c# - 为什么这段代码不打印任何东西?

转载 作者:行者123 更新时间:2023-11-30 19:35:58 25 4
gpt4 key购买 nike

我正在尝试创建一个工作程序,您必须在其中输入城镇、产品和数量并输出总价。

例如 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com