2016-04-03 142 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); 
}