2016-04-03 137 views
0

我正在創建一個計算不同日子裏不同水果價格的程序。我現在有兩種水果(香蕉和蘋果)。當我輸入「蘋果」時,星期幾和數量只給出蘋果的價格。問題是,當我輸入「香蕉」時,工作日和數量給了我香蕉和蘋果的價格。(C#)If-Else Statement Issue Issue

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace FruitShop 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var fruit = Console.ReadLine(); 
      var weekday = Console.ReadLine(); 
      var quantuty = double.Parse(Console.ReadLine()); 

      if (fruit == "banana") 
      { 
       if (weekday != "saturday" && weekday != "sunday") 
        Console.WriteLine(quantuty * 2.50); 

       if (weekday == "saturday") 
       { 
        Console.WriteLine(quantuty * 2.70); 
       } 
       if (weekday == "sunday") 
       { 
        Console.WriteLine(quantuty * 2.70); 
       } 
      } 






      else if (fruit == "apple") 
      { 
       if (weekday != "saturday" && weekday != "sunday") 
        Console.WriteLine(quantuty * 1.20); 
      } 

      { 
       if (weekday == "saturday") 
       { 
        Console.WriteLine(quantuty * 1.25); 
       } 
       if (weekday == "sunday") 
       { 
        Console.WriteLine(quantuty * 1.25); 
       } 

      } 


     } 
    } 
} 
+0

您是週六和週日病例不在蘋果內部 - 如果條件大括號。 – Filburt

+0

謝謝!它現在有效!我會再次檢查我的語法:) – HrNik

+1

使用[StyleCop](https://stylecop.codeplex.com/)(免費)或ReSharper(付費) - 他們會指出模糊的格式,可能會導致意想不到的行爲。 – Filburt

回答

2

我注意到你在使用大括號時犯了一個錯誤。你可以看到,當你看別的聲明。所有對應於「apple」的if語句都應該位於else-else括號內。我不知道,如果你通過我的描述了它,但要儘量看到它的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace FruitShop 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Type a fruit name:"); 
      var fruit = Console.ReadLine(); 

      Console.WriteLine("Type a week's day:"); 
      var weekday = Console.ReadLine(); 

      Console.WriteLine("Type a quantity:"); 
      var quantuty = double.Parse(Console.ReadLine()); 

      if (fruit == "banana") 
      { 
       if (weekday != "saturday" && weekday != "sunday") 
        Console.WriteLine(quantuty * 2.50); 

       if (weekday == "saturday") 
       { 
        Console.WriteLine(quantuty * 2.70); 
       } 
       if (weekday == "sunday") 
       { 
        Console.WriteLine(quantuty * 2.70); 
       } 
      } 
      //Here. All the if's should be inside this main else-if 
      else if (fruit == "apple") 
      { 
       if (weekday != "saturday" && weekday != "sunday") 
       { 
        Console.WriteLine(quantuty * 1.20); 
       } 

       if (weekday == "saturday") 
       { 
        Console.WriteLine(quantuty * 1.25); 
       } 
       if (weekday == "sunday") 
       { 
        Console.WriteLine(quantuty * 1.25); 
       } 
      } 


     } 
    } 
} 

我希望我解決您的查詢

0

if (fruit == "banana") 
{ 
    if (weekday == "saturday") 
     Console.WriteLine(quantuty * 2.70); 
    else if (weekday == "sunday") 
     Console.WriteLine(quantuty * 2.70); 
    else 
     Console.WriteLine(quantuty * 2.50); 
} 
else if (fruit == "apple") 
{ 
    if (weekday == "saturday") 
     Console.WriteLine(quantuty * 1.25); 
    else if (weekday == "sunday") 
     Console.WriteLine(quantuty * 1.25); 
    else 
     Console.WriteLine(quantuty * 1.20); 
}