C#哪里出错了

时间:2016-04-11 23:13:55

标签: c#

这是我已经提交的练习,我想更好地了解我哪里出错了。该程序应该计算出我们可以吃多少糖果以及剩下多少包装,鉴于有促销,我们可以获得X包装,我们可以获得Y免费糖果,然后我们可以使用这些包裹来获得额外的免费糖果直到我们没有达到获得更多所需的金额。

我的解决方案适用于多种情况但不适用于某些情况,如:

4包装获得2个免费赠品,买8个= 13个吃掉,5个WrapLeft(应该14个吃掉,2个WrapsLeft)。

我已经多次阅读过该代码,无法弄清楚我是否设计了这些公式,还是编码错误(尽管它编译好了)。我被困了。

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

namespace candy
{
    class Program
    {
        static void Main(string[] args)
        {
            int Wrap = 0;
            int Freebie = 0;
            int TotalFreebie = 0;
            int Buy = 0;
            int Increment = 0;
            int Eaten = 0;
            int WrapsLeft = 0;

            Console.WriteLine("Introduce amount of wraps");
            Wrap = Int32.Parse(Console.ReadLine());
            Console.WriteLine("Introduce number of freebies per wrap");
            Freebie = Int32.Parse(Console.ReadLine());
            Console.WriteLine("Introduce amount of candy bought");
            Buy = Int32.Parse(Console.ReadLine());

            TotalFreebie = (Buy / Wrap) * Freebie;
            Eaten = Buy + TotalFreebie;
            WrapsLeft = Eaten - Buy;

            if (Freebie > Buy || Freebie >= Wrap)
            {
                Console.WriteLine("COMPANY GOES BANKRUPT");
                Console.ReadLine();
            }
            else
            {
                if (TotalFreebie > Wrap)
                {
                    do
                    {
                        TotalFreebie = (Buy / Wrap) * Freebie;
                        Increment = TotalFreebie / Wrap;
                        Increment++;
                        Eaten = Buy + TotalFreebie + Increment;
                        WrapsLeft = Eaten - (Buy + TotalFreebie);

                    } while (TotalFreebie > Wrap);
                }

                else 
                {
                    if (TotalFreebie == Wrap && Buy > Wrap)
                    {
                        TotalFreebie = (Buy / Wrap) * Freebie;
                        Eaten = Buy + TotalFreebie + Freebie;
                        WrapsLeft = Eaten - (Buy + TotalFreebie);
                    }

                    else
                    {
                        TotalFreebie = (Buy / Wrap) * Freebie;
                        Eaten = Buy + TotalFreebie;
                        WrapsLeft = Eaten - Buy;
                    }
                }

                TotalFreebie = (Buy / Wrap) * Freebie;
                Eaten = Buy + TotalFreebie + (TotalFreebie / Wrap);

                if (TotalFreebie > Wrap)
                {
                    WrapsLeft = Eaten - (Buy + TotalFreebie);
                }

                else
                {
                    WrapsLeft = Eaten - Buy;
                }

                Console.WriteLine("{0} {1}", Eaten, WrapsLeft);
                Console.ReadLine();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

只是为了告诉你哪里出错了...如果用户输入符合你提到的情景,请查看流程...

//For 4 Wraps get 2 Freebies, Buy 8 = 13 Eaten and 5 WrapLeft (should be 14 Eaten and 2 WrapsLeft).
static void Main(string[] args)
{
    int Wrap = 0;
    int Freebie = 0;
    int TotalFreebie = 0;
    int Buy = 0;
    int Increment = 0;
    int Eaten = 0;
    int WrapsLeft = 0;

    Console.WriteLine("Introduce amount of wraps");
    Wrap = Int32.Parse(Console.ReadLine()); // 4
    Console.WriteLine("Introduce number of freebies per wrap");
    Freebie = Int32.Parse(Console.ReadLine()); // 2
    Console.WriteLine("Introduce amount of candy bought");
    Buy = Int32.Parse(Console.ReadLine()); // 8

    TotalFreebie = (Buy / Wrap) * Freebie; // 8/4*2 = 4
    Eaten = Buy + TotalFreebie; // 8+4 = 12
    WrapsLeft = Eaten - Buy; 12-8 = 4

    if (Freebie > Buy || Freebie >= Wrap) // 2 > 8 || 2 >= 4 *FALSE*
    {
        Console.WriteLine("COMPANY GOES BANKRUPT");
        Console.ReadLine();
    }

    else
    {
        if (TotalFreebie > Wrap) // 4 > 4 *TRUE*
        {
            do
            {
                TotalFreebie = (Buy / Wrap) * Freebie;
                Increment = TotalFreebie / Wrap;
                Increment++;
                Eaten = Buy + TotalFreebie + Increment;
                WrapsLeft = Eaten - (Buy + TotalFreebie);

            } while (TotalFreebie > Wrap);
        }

        else 
        {
            if (TotalFreebie == Wrap && Buy > Wrap) // 4 == 4 && 8 > 4 *TRUE*
            {
                TotalFreebie = (Buy / Wrap) * Frebie; // 8/4*2 = 4
                Eaten = Buy + TotalFreebie + Freebie; // 8+4+2 = 14
                WrapsLeft = Eaten - (Buy + TotalFreebie); // 14-(8+4) = 2
            }

            else 
            {
                TotalFreebie = (Buy / Wrap) * Freebie;
                Eaten = Buy + TotalFreebie;
                WrapsLeft = Eaten - Buy;
            }
        }

        TotalFreebie = (Buy / Wrap) * Freebie; // 8/4*2 = 4
        Eaten = Buy + TotalFreebie + (TotalFreebie/Wrap); // 8+4+(4/4) = 13 

        if (TotalFreebie>Wrap) // 4>4 *FALSE*
        {
            WrapsLeft = Eaten - (Buy + TotalFreebie);
        }

        else
        {
            WrapsLeft = Eaten - Buy; // 13-8 = 5 
        }



        Console.WriteLine("{0} {1}", Eaten, WrapsLeft); // 13 5
        Console.ReadLine();
    }
}

以下是修改后的版本...由于我删除了一些代码,您可能需要验证最初的错误处理是否仍适用。

//For 4 Wraps get 2 Freebies, Buy 8 = 13 Eaten and 5 WrapLeft (should be 14 Eaten and 2 WrapsLeft).
static void Main(string[] args)
{
    int Wrap = 0;
    int Freebie = 0;
    int TotalFreebie = 0;
    int Buy = 0;
    int Increment = 0;
    int Eaten = 0;
    int WrapsLeft = 0;

    Console.WriteLine("Introduce amount of wraps");
    Wrap = Int32.Parse(Console.ReadLine()); // 4
    Console.WriteLine("Introduce number of freebies per wrap");
    Freebie = Int32.Parse(Console.ReadLine()); // 2
    Console.WriteLine("Introduce amount of candy bought");
    Buy = Int32.Parse(Console.ReadLine()); // 8


    if (Freebie > Buy || Freebie >= Wrap) // 2 > 8 || 2 >= 4 *FALSE*
    {
        Console.WriteLine("COMPANY GOES BANKRUPT");
        Console.ReadLine();
    }

    else
    {
        TotalFreebie = (Buy/Wrap)*Freebie; // (8/4)*2 = 4

        WrapsLeft = Buy%Wrap + TotalFreebie; = 8%4 + 4 = 0+4 = 4

        while(WrapsLeft >= Wrap) // 4>=4 *TRUE* // 2>=4 *FALSE*
        {
            TotalFreebie += (WrapsLeft/Wrap)*Freebie; // 4 + (4/4)*2 = 4 + 2 = 6
            WrapsLeft = (WrapsLeft/Wrap)*Freebie; // (4/4)*2 = 2
        }            

        Eaten = Buy + TotalFreebie; // 8+6 = 14

        Console.WriteLine("{0} {1}", Eaten, WrapsLeft); // 14 2
        Console.ReadLine();
    }
}

答案 1 :(得分:0)

您的代码存在一些问题。

使用Int32.Parse(Console.ReadLine());设置的三个变量只能设置一次。在初始输入后,它们不会在整个代码中发生变化。所以,看看这个循环:

        do
        {
            TotalFreebie = (Buy_SETONCE / Wrap_SETONCE) * Freebie_SETONCE;
            Increment = TotalFreebie / Wrap_SETONCE;
            Increment++;
            Eaten = Buy_SETONCE + TotalFreebie + Increment;
            WrapsLeft = Eaten - (Buy_SETONCE + TotalFreebie);
        } while (TotalFreebie > Wrap_SETONCE);

您将看到设置一次的变量,因为我重命名后,_SETONCE附加到其名称。您将看到,在循环的每次迭代中,TotalFreebie将是一个常量值,因为它仅由设置一次的值组成。这意味着,如果TotalFreebie > Wrap_SETONCEtrue,那么您永远不会离开循环。

如果你没有进入循环,那么你会点击if的另一面,它会设置TotalFreebieEaten和& WrapsLeft,但在此代码运行后立即再次设置这三个变量:

    TotalFreebie = (Buy_SETONCE / Wrap_SETONCE) * Freebie_SETONCE;
    Eaten = Buy_SETONCE + TotalFreebie + (TotalFreebie / Wrap_SETONCE);

    if (TotalFreebie > Wrap_SETONCE)
    {
        WrapsLeft = Eaten - (Buy_SETONCE + TotalFreebie);
    }
    else
    {
        WrapsLeft = Eaten - Buy_SETONCE;
    }

所以,有效的代码看起来像这样:

Console.WriteLine("Introduce amount of wraps");
Wrap_SETONCE = Int32.Parse(Console.ReadLine());
Console.WriteLine("Introduce number of freebies per wrap");
Freebie_SETONCE = Int32.Parse(Console.ReadLine());
Console.WriteLine("Introduce amount of candy bought");
Buy_SETONCE = Int32.Parse(Console.ReadLine());

TotalFreebie = (Buy_SETONCE / Wrap_SETONCE) * Freebie_SETONCE;
Eaten = Buy_SETONCE + TotalFreebie;
WrapsLeft = Eaten - Buy_SETONCE;

if (Freebie_SETONCE > Buy_SETONCE || Freebie_SETONCE >= Wrap_SETONCE)
{
    Console.WriteLine("COMPANY GOES BANKRUPT");
    Console.ReadLine();
}
else
{
    TotalFreebie = (Buy_SETONCE / Wrap_SETONCE) * Freebie_SETONCE;
    Eaten = Buy_SETONCE + TotalFreebie + (TotalFreebie / Wrap_SETONCE);

    if (TotalFreebie > Wrap_SETONCE)
    {
        WrapsLeft = Eaten - (Buy_SETONCE + TotalFreebie);
    }
    else
    {
        WrapsLeft = Eaten - Buy_SETONCE;
    }

    Console.WriteLine("{0} {1}", Eaten, WrapsLeft);
    Console.ReadLine();
}

现在测试公司是否破产的if不依赖于TotalFreebieEaten和&的初始值。 WrapsLeft个变量,因此您可以将这些计算移到if的后半部分。

但这会导致一些不必要的重复。

假设我可以忽略错误的do循环,我最终可能会像这样编写整个代码:

Console.WriteLine("Introduce amount of wraps");
int Wrap = Int32.Parse(Console.ReadLine());
Console.WriteLine("Introduce number of freebies per wrap");
int Freebie = Int32.Parse(Console.ReadLine());
Console.WriteLine("Introduce amount of candy bought");
int Buy = Int32.Parse(Console.ReadLine());

if (Freebie > Buy || Freebie >= Wrap)
{
    Console.WriteLine("COMPANY GOES BANKRUPT");
}
else
{
    int TotalFreebie = (Buy / Wrap) * Freebie;
    int Eaten = Buy + TotalFreebie + (TotalFreebie / Wrap);
    int WrapsLeft = Eaten - Buy - (TotalFreebie > Wrap ? TotalFreebie : 0);
    Console.WriteLine("{0} {1}", Eaten, WrapsLeft);
}
Console.ReadLine();