gpt4 book ai didi

c# - 变量不会响应输入而更改其值

转载 作者:太空宇宙 更新时间:2023-11-03 18:57:14 26 4
gpt4 key购买 nike

我实际上对编程很陌生,也许我只是累了,但我找不到这里的问题。 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");
}
}

最佳答案

您将 city 转换为小写,然后使用大写首字母进行比较!那不会有好结果。

即你需要

if (city == "sofia")

等等

您的逐行调试器可用于验证这一点。请花一些时间学习如何使用它。调试比能够键入代码更重要。

关于c# - 变量不会响应输入而更改其值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42111090/

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