任何人都可以帮助调试我对此程序的问题

时间:2017-01-11 04:06:58

标签: c# debugging

由于某种原因,我创建的并行数组彼此不对应。当我输入披萨的大小时,它会输出数组中的第一个变量。谢谢!

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

    namespace Pizza_Prices_2
    {
        class Program
        {
            static void Main(string[] args)
            {

            char[] PizzaSizes = new char[] { 'S', 'M', 'L', 'X' };
            double[] PizzaPrices = new double[] { 6.99, 8.99, 12.50, 15.00 };
            int index = 0, NumOfPizzas = 0;
            double Discount = 0.0, TotalPizzaPrice = 0.0;
            string PizzaSizeAsString, NumOfPIzzasAsString;
            char PizzaSizeChar;
            Console.Write("What pizza size do you want? ");
            PizzaSizeAsString = Console.ReadLine();
            PizzaSizeChar = Convert.ToChar(PizzaSizeAsString);
            Console.Write("How many pizzas do you want");
            NumOfPIzzasAsString = Console.ReadLine();
            NumOfPizzas = Convert.ToInt32(NumOfPIzzasAsString);
            foreach (char PizzaSize in PizzaSizes)

                if (PizzaSize == PizzaSizeChar)
                {
                        TotalPizzaPrice = (PizzaPrices[index] * NumOfPizzas);
                        Console.WriteLine("Your {0} pizza would normally be {1}",      PizzaSize, PizzaPrices[index].ToString("C"));
                        Console.WriteLine("Your total would {0}", TotalPizzaPrice.ToString("C"));
                }
            if (NumOfPizzas == 2)
            {
                Discount = 0.10;
                Console.WriteLine("Because you ordered {1} pizzas, your discount is {0}", Discount.ToString("P1"), NumOfPizzas);
                Console.WriteLine("For a final total of {0}", (TotalPizzaPrice * (1 - Discount)).ToString("C"));
            }
            if (NumOfPizzas == 3 || NumOfPizzas == 4)
            {
                Discount = 0.15;
                Console.WriteLine("Because you ordered {1} pizzas, your discount is {0}", Discount.ToString("P1"), NumOfPizzas);
                Console.WriteLine("For a final total of {0}", (TotalPizzaPrice * (1 - Discount)).ToString("C"));
            }

            if (NumOfPizzas >= 5)
            {
                Discount = 0.20;


                Console.WriteLine("Because you ordered {1} pizzas, your discount is {0}", Discount.ToString("P1"), NumOfPizzas);
                Console.WriteLine("For a final total of {0}", (TotalPizzaPrice * (1 - Discount)).ToString("C"));
            }




        }
    }
}

我真的不知道问题是什么。如果有人可以帮我解决这个问题,那将会非常有帮助。

3 个答案:

答案 0 :(得分:0)

你的问题在于这一行

TotalPizzaPrice = (PizzaPrices[index] * NumOfPizzas);

您可以看到PizzaPrices访问index数组。 index的值始终为0,因为它永远不会设置为其他值。快速修复是为每次迭代增加index的值。

foreach (char PizzaSize in PizzaSizes)
{
    if (PizzaSize == PizzaSizeChar)
    {
        TotalPizzaPrice = (PizzaPrices[index] * NumOfPizzas);
        Console.WriteLine("Your {0} pizza would normally be {1}", PizzaSize, PizzaPrices[index].ToString("C"));
        Console.WriteLine("Your total would {0}", TotalPizzaPrice.ToString("C"));
    }

    index++;
}

虽然最好能够使用Dictionary作为价格并使用单独的商业方法来获得折扣,但不要重复太多代码。

class Program
{
    void Main()
    {
        Dictionary<char, double> Prices = new Dictionary<char, double>
        {
            {'S', 6.99},
            {'M', 8.99},
            {'L', 12.50},
            {'X', 15}
        };

        int NumOfPizzas = 0;
        double Discount = 0.0, TotalPizzaPrice = 0.0;
        string PizzaSizeAsString, NumOfPIzzasAsString;

        char PizzaSizeChar;

        Console.Write("What pizza size do you want? ");
        PizzaSizeAsString = Console.ReadLine();
        PizzaSizeChar = Convert.ToChar(PizzaSizeAsString);

        Console.Write("How many pizzas do you want");
        NumOfPIzzasAsString = Console.ReadLine();
        NumOfPizzas = Convert.ToInt32(NumOfPIzzasAsString);

        TotalPizzaPrice = Prices[PizzaSizeChar] * NumOfPizzas;
        Console.WriteLine("Your {0} pizza would normally be {1}", PizzaSizeChar, Prices[PizzaSizeChar]);
        Console.WriteLine("Your total would {0}", TotalPizzaPrice.ToString("C"));

        Discount = GetDiscount(NumOfPizzas);
        if (Discount > 0)
        {
            Console.WriteLine("Because you ordered {1} pizzas, your discount is {0}", Discount.ToString("P1"), NumOfPizzas);
            Console.WriteLine("For a final total of {0}", (TotalPizzaPrice * (1 - Discount)).ToString("C"));
        }   
    }

    public double GetDiscount(int amount)
    {
        if(amount == 2) return 0.1;
        if(amount == 3 || amount == 4) return 0.15;
        if(amount >= 5) return 0.2;

        return 0;
    }
}

答案 1 :(得分:0)

我建议您使用this代替Dictionary<char,double>char[]的组合,这样您就可以轻松处理用户输入:看看进入下面的代码:

double[]

答案 2 :(得分:0)

请检查这是否正常

 static void Main(string[] args)
    {

    char[] PizzaSizes = new char[] { 'S', 'M', 'L', 'X' };
    double[] PizzaPrices = new double[] { 6.99, 8.99, 12.50, 15.00 };
    int index = 0, NumOfPizzas = 0;
    double Discount = 0.0, TotalPizzaPrice = 0.0;
    string PizzaSizeAsString, NumOfPIzzasAsString;
    char PizzaSizeChar;
    Console.Write("What pizza size do you want? ");
    PizzaSizeAsString = Console.ReadLine();
    PizzaSizeChar = Convert.ToChar(PizzaSizeAsString);
    index =   Array.IndexOf(PizzaSizes, PizzaSizeChar);
    Console.Write("How many pizzas do you want");
    NumOfPIzzasAsString = Console.ReadLine();
    NumOfPizzas = Convert.ToInt32(NumOfPIzzasAsString);
    foreach (char PizzaSize in PizzaSizes)

        if (PizzaSize == PizzaSizeChar)
        {
                TotalPizzaPrice = (PizzaPrices[index] * NumOfPizzas);
                Console.WriteLine("Your {0} pizza would normally be {1}",      PizzaSize, PizzaPrices[index].ToString("C"));
                Console.WriteLine("Your total would {0}", TotalPizzaPrice.ToString("C"));
        }
    if (NumOfPizzas == 2)
    {
        Discount = 0.10;
        Console.WriteLine("Because you ordered {1} pizzas, your discount is {0}", Discount.ToString("P1"), NumOfPizzas);
        Console.WriteLine("For a final total of {0}", (TotalPizzaPrice * (1 - Discount)).ToString("C"));
    }
    if (NumOfPizzas == 3 || NumOfPizzas == 4)
    {
        Discount = 0.15;
        Console.WriteLine("Because you ordered {1} pizzas, your discount is {0}", Discount.ToString("P1"), NumOfPizzas);
        Console.WriteLine("For a final total of {0}", (TotalPizzaPrice * (1 - Discount)).ToString("C"));
    }

    if (NumOfPizzas >= 5)
    {
        Discount = 0.20;


        Console.WriteLine("Because you ordered {1} pizzas, your discount is {0}", Discount.ToString("P1"), NumOfPizzas);
        Console.WriteLine("For a final total of {0}", (TotalPizzaPrice * (1 - Discount)).ToString("C"));
    }
    }

我只设置像这样的索引值

index =   Array.IndexOf(PizzaSizes, PizzaSizeChar);