Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 变量不';t根据输入改变其值_C# - Fatal编程技术网

C# 变量不';t根据输入改变其值

C# 变量不';t根据输入改变其值,c#,C#,事实上,我对编程很陌生,也许我只是有点累,但我在这里找不到问题。结果似乎没有改变其值。如您所见,第一个输入是产品,第二个输入是城市,最后是我们想要的该产品的金额。程序必须打印最终价格(结果) 将city转换为小写,然后使用大写首字母进行比较!这不会有好的结局 i、 你需要 if(城市==“索菲亚”) 等等 您的逐行调试器可用于验证这一点。一定要花点时间学习如何使用它。调试比能够键入代码更重要。您将城市转换为小写,然后使用大写首字母进行比较!这不会有好的结局 i、 你需要 if(城市==“索菲亚”

事实上,我对编程很陌生,也许我只是有点累,但我在这里找不到问题。
结果
似乎没有改变其值。如您所见,第一个输入是
产品
,第二个输入是
城市
,最后是我们想要的该产品的
金额
。程序必须打印最终价格(
结果


city
转换为小写,然后使用大写首字母进行比较!这不会有好的结局

i、 你需要

if(城市==“索菲亚”)

等等


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

您将
城市
转换为小写,然后使用大写首字母进行比较!这不会有好的结局

i、 你需要

if(城市==“索菲亚”)

等等


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

正如芭丝谢芭所说,您也需要将比较改为小写,因为比较是精确的。另外,我建议放置一个
Console.ReadKey()在最后,这样在得到结果之前它不会关闭

例如:

    Console.Write("Product: ");
    string product = Console.ReadLine().ToLower();
    Console.Write("City: ");
    string city = Console.ReadLine().ToLower();
    Console.Write("Price: ");
    double amount = Convert.ToDouble(Console.ReadLine());
    double result;
    if (city == "sofia")
    {
        if (product == "coffee")
        {
            result = amount * 0.50;
            Console.WriteLine(result);
        }
    }
    Console.ReadKey();
    }
}
我把
Console.Write()在运行时有可读性,所以我可以测试它

对于许多
if
,我建议使用switch语句

例如


正如芭丝谢芭所说,你也需要将比较改为小写,因为比较是精确的。另外,我建议放置一个
Console.ReadKey()在最后,这样在得到结果之前它不会关闭

例如:

    Console.Write("Product: ");
    string product = Console.ReadLine().ToLower();
    Console.Write("City: ");
    string city = Console.ReadLine().ToLower();
    Console.Write("Price: ");
    double amount = Convert.ToDouble(Console.ReadLine());
    double result;
    if (city == "sofia")
    {
        if (product == "coffee")
        {
            result = amount * 0.50;
            Console.WriteLine(result);
        }
    }
    Console.ReadKey();
    }
}
我把
Console.Write()在运行时有可读性,所以我可以测试它

对于许多
if
,我建议使用switch语句

例如


有些人已经看到了小写的问题。但也可以作为嵌套的替代方案。为了干净的代码,请更改为switch语句以减少嵌套并提高代码的可读性

 var product = "coffee";
            var city = "VarNa";
            var amount = 10.00;
            double result;

            switch (city.ToLower())
            {
                case "sofia":
                    switch (product)
                    {
                        case "coffee":
                            result = amount * 0.50;
                            Console.WriteLine(result);
                            break;
                        case "water":
                            result = amount * 0.80;
                            Console.WriteLine(result);
                            break;
                        case "beer":
                            result = amount * 1.20;
                            Console.WriteLine(result);
                            break;
                        case "sweets":
                            result = amount * 1.45;
                            Console.WriteLine(result);
                            break;
                        case "peanuts":
                            result = amount * 1.60;
                            Console.WriteLine(result);
                            break;
                    }
                    break;

                case "plovdiv":
                    switch (product)
                    {
                        case "coffee":
                            result = amount * 0.40;
                            Console.WriteLine(result);
                            break;
                        case "water":
                            result = amount * 0.70;
                            Console.WriteLine(result);
                            break;
                        case "beer":
                            result = amount * 1.15;
                            Console.WriteLine(result);
                            break;
                        case "sweets":
                            result = amount * 1.30;
                            Console.WriteLine(result);
                            break;
                        case "peanuts":
                            result = amount * 1.50;
                            Console.WriteLine(result);
                            break;
                    }
                    break;

                case "varna":
                    switch (product)
                    {
                        case "coffee":
                            result = amount * 0.45;
                            Console.WriteLine(result);
                            break;
                        case "water":
                            result = amount * 0.70;
                            Console.WriteLine(result);
                            break;
                        case "beer":
                            result = amount * 1.10;
                            Console.WriteLine(result);
                            break;
                        case "sweets":
                            result = amount * 1.35;
                            Console.WriteLine(result);
                            break;
                        case "peanuts":
                            result = amount * 1.55;
                            Console.WriteLine(result);
                            break;
                        default:
                            Console.WriteLine("Invalid");
                            break;
                    }
                    break;
            }

有些人已经看到了小写的问题。但也可以作为嵌套的替代方案。为了干净的代码,请更改为switch语句以减少嵌套并提高代码的可读性

 var product = "coffee";
            var city = "VarNa";
            var amount = 10.00;
            double result;

            switch (city.ToLower())
            {
                case "sofia":
                    switch (product)
                    {
                        case "coffee":
                            result = amount * 0.50;
                            Console.WriteLine(result);
                            break;
                        case "water":
                            result = amount * 0.80;
                            Console.WriteLine(result);
                            break;
                        case "beer":
                            result = amount * 1.20;
                            Console.WriteLine(result);
                            break;
                        case "sweets":
                            result = amount * 1.45;
                            Console.WriteLine(result);
                            break;
                        case "peanuts":
                            result = amount * 1.60;
                            Console.WriteLine(result);
                            break;
                    }
                    break;

                case "plovdiv":
                    switch (product)
                    {
                        case "coffee":
                            result = amount * 0.40;
                            Console.WriteLine(result);
                            break;
                        case "water":
                            result = amount * 0.70;
                            Console.WriteLine(result);
                            break;
                        case "beer":
                            result = amount * 1.15;
                            Console.WriteLine(result);
                            break;
                        case "sweets":
                            result = amount * 1.30;
                            Console.WriteLine(result);
                            break;
                        case "peanuts":
                            result = amount * 1.50;
                            Console.WriteLine(result);
                            break;
                    }
                    break;

                case "varna":
                    switch (product)
                    {
                        case "coffee":
                            result = amount * 0.45;
                            Console.WriteLine(result);
                            break;
                        case "water":
                            result = amount * 0.70;
                            Console.WriteLine(result);
                            break;
                        case "beer":
                            result = amount * 1.10;
                            Console.WriteLine(result);
                            break;
                        case "sweets":
                            result = amount * 1.35;
                            Console.WriteLine(result);
                            break;
                        case "peanuts":
                            result = amount * 1.55;
                            Console.WriteLine(result);
                            break;
                        default:
                            Console.WriteLine("Invalid");
                            break;
                    }
                    break;
            }

你能看到代码有多好斗吗?让我们将实现分为模型和业务逻辑(C#6.0):


你能看到代码有多好斗吗?让我们将实现分为模型和业务逻辑(C#6.0):


哦,我还以为没关系呢!谢谢:)如果你做了
city.Equals(“SoFiA”,StringComparison.InvariantCultureInogoreCase)就没关系了。
哦,我还以为没关系呢!谢谢:)如果你使用
city.Equals(“SoFiA”,StringComparison.InvariantCultureInogoreCase)这似乎很方便:)这似乎很方便:)C#6.0+。你应该提一下。C#6.0+。你应该提一下。
// Dictionary of dictionary; a better solution is to implement a custom class for it 
private static Dictionary<string, Dictionary<string, double>> s_Model = 
  new Dictionary<string, Dictionary<string, double>>(StringComparer.OrdinalIgnoreCase) {
    { "Sofia", new Dictionary<string, double>(StringComparer.OrdinalIgnoreCase) {
      { "coffee", 0.80},
      { "beer", 1.20},
      { "water", 0.50} } },

    { "Plovdiv", new Dictionary<string, double>(StringComparer.OrdinalIgnoreCase) {
      { "coffee", 0.70},
      { "water", 0.45} } },
  };
static void Main(string[] args)
{
    // It's the Model that should take case into account
    string product = Console.ReadLine();
    string city = Console.ReadLine();
    double amount = double.Parse(Console.ReadLine());

    Dictionary<string, double> products = null;
    double price;

    if (!s_Model.TryGetValue(city, out products)) 
      Console.WriteLine("Incorrect city"); 
    else if (products.TryGetValue(products, out price))
      Console.WriteLine("Incorrect product");
    else
      Console.Write((amount * price).ToString("F2"));
}