变量不会根据输入更改其值

Ann*_*nna 1 c#

我其实很喜欢编程,也许我只是累了,但我在这里找不到问题.本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");
        }
    }
Run Code Online (Sandbox Code Playgroud)

Bat*_*eba 5

您转换city为小写,然后使用大写首字母进行比较!这不会很好.

即你需要

if (city == "sofia")

等等

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