2017-02-08 13 views
1

私は実際にはプログラミングに新しいですし、たぶん疲れているかもしれませんが、ここで問題を見つけることはできません。 resultは値を変更していないようです。ご覧のとおり、最初の入力はproductで、2番目の入力は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"); 
     } 
    } 

答えて

5

cityを小文字に変換し、大文字の最初の文字を使用して比較します。それはうまく終わらないでしょう。

つまり、あなたが

if (city == "sofia")

など

を必要とするラインデバッガによってあなたのラインはこれを確認するために使用することができます。それを使う方法を学ぶのに時間を費やしています。デバッグは、コードをタイプすることよりも重要です。

+0

ああ、私はそれは問題ではありませんでしたと思いました!ありがとう:) – Anna

+0

あなたは 'city.Equals(" SoFiA "、StringComparison.InvariantCultureIgnoreCase)' –

0

Bathshebaによると、比較が正確であるため、比較も小文字に変更する必要があります。また、結果が出る前に閉じないように、最後に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(city) 
     { 
      case "sofia": 
       switch (product) 
       { 
        case "coffee": 
         result = amount * 0.50; 
         Console.WriteLine(result); 
         break; 
       } 
       break; 

      case "plovdiv": 
       switch (product) 
       { 
        case "coffee": 
         result = amount * 0.40; 
         Console.WriteLine(result); 
         break; 
       } 
       break; 
     } 
+0

を実行すると問題は起こりません。これは便利です:) – Anna

0

誰かが小文字の問題を既に認識しています。あなたが持っている入れ子の代わりに使うこともできます。クリーンなコードのために、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; 
      } 
1

コードとのコンビネーションを確認できますか?レッツスプリットモデルビジネスロジック(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} } }, 
    }; 

だけモデルを使用したより。提供されたcityproductsを取得してみてください。その後、必要にproductためpriceを探して:

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")); 
} 
+0

C#6.0+。それに言及する必要があります。 –

関連する問題