(C#)If-Else Statement Issue

时间:2016-04-03 18:01:05

标签: c#

我正在创建一个计算不同日期不同水果价格的程序。我现在有两种水果(香蕉和苹果)。当我输入“apple”时,工作日和数量只给苹果的价格。问题是,当我输入“banana”时,工作日和数量会给我香蕉和苹果的价格。

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);
                }

            }


        }
    }
}

2 个答案:

答案 0 :(得分:2)

我注意到你在使用牙箍时犯了一个错误。你可以看到,当你看到其他声明时。对应于“apple”的所有if语句都应该在else-if括号内。我不知道你是否通过我的描述得到它,但试着在代码上看到它:

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);
                 }
            }


        }
    }
} 

我希望我能解决您的问题

答案 1 :(得分: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);
}