执行计算时 C# 变量不会更改

C# Variable Doesn't Change When The Calculation is Performed

我正在为我的 C# 创建一个简单的计算器 class,但在最后的计算中遇到了一些问题。

我已经声明了我的变量,并且我正在为订单使用简单的 if then 语句。我的问题是,在我定义了我的变量之后,它们包含的信息在执行计算时不会改变,它们只是输出原始值定义的零,而不是从 if 语句(TotalCostTaxTotalWithTax)。

我输入了不同的数字来代替零并且公式有效,只是该值永远不会更改为我在

中声明的值
  1. If 语句。是否无法从 if 语句中调整变量的值?
  2. 如果是这样的话,另一种可能的方法是什么?

尽量保持简单,因为我并不真正精通控制台编程。

        string SizeName = "";
        double SizeCost = 0;
        string ToppingName = "";
        double ToppingCost = 0;
        double TotalCost = (SizeCost + ToppingCost);
        double Tax = (TotalCost*0.085);
        double TotalWithTax = (TotalCost + Tax);

        //Print a greeting message.
        Console.WriteLine("Welcome to the Central Pizza Parlor!");

        //Ask if the customer would like to order a pizza.
        Console.WriteLine("Would you like to order a pizza today? Enter y for Yes or n for No.");
        string Order = Console.ReadLine();

        //Start the order if answer is Yes, if not, then exit the program.
        if (Order == "y")
        {
            //Continue with order.
            Console.WriteLine("Great! Let's get started, please pick the size of your pizza:");
            Console.WriteLine("1 - Small  .00");
            Console.WriteLine("2 - Medium .00");
            Console.WriteLine("3 - Large  .00");

            //Get pizza size for order.
            Console.WriteLine("Please enter the number for the pizza size you would like.");
            string sizeAsAString = Console.ReadLine();
            int size = Convert.ToInt32(sizeAsAString);


            //Use If Else statement to set the variable value for SizeCost.
            if (size == 1)
            {
                SizeCost = 5.0;
                SizeName = ("Small");
            }
            else if (size == 2)
            {
                SizeCost = 7.0;
                SizeName = ("Medium");
            }
            else if (size == 3)
            {
                SizeCost = 9.0;
                SizeName = ("Large");
            }

            //Have Customer select toppings.
            Console.WriteLine("Please select which topping you would like on your pizza.");
            ;
            Console.WriteLine("1 - Pepperoni     .00");
            Console.WriteLine("2 - Ham           .00");
            Console.WriteLine("3 - Onions        .00");
            Console.WriteLine("4 - Mushrooms     .00");
            Console.WriteLine("5 - Green Peppers .00");

            Console.WriteLine("Please enter the number for the corresponding topping you would like.");
            string toppingAsAString = Console.ReadLine();
            int topping = Convert.ToInt32(toppingAsAString);


            //Use If Else statement to set the variable value for ToppingCost.
            if (topping == 1) 
            {
                ToppingCost = 2.0;
                ToppingName = ("Pepperoni");
            }
            else if (topping == 2)
            {
                ToppingCost = 2.0;
                ToppingName = ("Ham");
            }
            else if (topping == 3)
            {
                ToppingCost = 1.0;
                ToppingName = ("Onions");
            }
            else if (topping == 4)
            {
                ToppingCost = 1.0;
                ToppingName = ("Mushrooms");
            }
            else if (topping == 5)
            {
                ToppingCost = 1.0;
                ToppingName = "Green Peppers";
            }

            //Display order details.
            Console.WriteLine("Here are the details for your order.");
            Console.WriteLine("Thank you for your business!");
            Console.WriteLine("You can pick up your pizza in 25 minutes!");

            //Show current time of order.
            DateTime now = DateTime.Now;
            Console.WriteLine("Time Ordered: "+now+" ");

            //Show Current time of order with additional 25 minutes for pickup.
            DateTime pickup = DateTime.Now.AddMinutes(25);
            Console.WriteLine("Pick Up At: "+pickup+" ");

            //Output Pizza Size.
            Console.WriteLine("Size: " +SizeName+ "  ");

            //OutPut Topping name.
            Console.WriteLine("Topping: " +ToppingName+ " ");

            Console.WriteLine("---------------");

            //Output total price of size and topping chosen.
            Console.WriteLine("Pizza Price: $ "+TotalCost+" ");

            //Output tax amount.
            Console.WriteLine("Tax: $" +Tax+ " ");

            //Output total price with tax.
            Console.WriteLine("Total Price: $" +TotalWithTax+ "  ");

        }
        else 
        {
            //Exit the program because the customer does not want to order a pizza.
            Console.WriteLine("Alright, have a great day!");
        }

        Console.ReadLine();
    }
  }
}

您在更改 SizeCost 或 ToppingCost 的值之前设置了 TotalCost。

移动这些行

double TotalCost = (SizeCost + ToppingCost);
double Tax = (TotalCost*0.085);
double TotalWithTax = (TotalCost + Tax);

在您的输入部分之后。

你所有的计算都在顶部(在声明中)。使用 defaults 执行计算将不会输出所需的结果。

double TotalCost = (SizeCost + ToppingCost);
double Tax = (TotalCost*0.085);
double TotalWithTax = (TotalCost + Tax);

将这些计算移到底部(在输出之前)。

public class Program
{
    public static void Main()
    {       
          string SizeName = "";
        double SizeCost = 0;
        string ToppingName = "";
        double ToppingCost = 0;
        double TotalCost =0;
        double Tax = 0;
        double TotalWithTax = 0;

        //Print a greeting message.
        Console.WriteLine("Welcome to the Central Pizza Parlor!");

        //Ask if the customer would like to order a pizza.
        Console.WriteLine("Would you like to order a pizza today? Enter y for Yes or n for No.");
        string Order = Console.ReadLine();

        //Start the order if answer is Yes, if not, then exit the program.
        if (Order == "y")
        {
            //Continue with order.
            Console.WriteLine("Great! Let's get started, please pick the size of your pizza:");
            Console.WriteLine("1 - Small  .00");
            Console.WriteLine("2 - Medium .00");
            Console.WriteLine("3 - Large  .00");

            //Get pizza size for order.
            Console.WriteLine("Please enter the number for the pizza size you would like.");
            string sizeAsAString = Console.ReadLine();
            int size = Convert.ToInt32(sizeAsAString);


            //Use If Else statement to set the variable value for SizeCost.
            if (size == 1)
            {
                SizeCost = 5.0;
                SizeName = ("Small");
            }
            else if (size == 2)
            {
                SizeCost = 7.0;
                SizeName = ("Medium");
            }
            else if (size == 3)
            {
                SizeCost = 9.0;
                SizeName = ("Large");
            }

            //Have Customer select toppings.
            Console.WriteLine("Please select which topping you would like on your pizza.");
            ;
            Console.WriteLine("1 - Pepperoni     .00");
            Console.WriteLine("2 - Ham           .00");
            Console.WriteLine("3 - Onions        .00");
            Console.WriteLine("4 - Mushrooms     .00");
            Console.WriteLine("5 - Green Peppers .00");

            Console.WriteLine("Please enter the number for the corresponding topping you would like.");
            string toppingAsAString = Console.ReadLine();
            int topping = Convert.ToInt32(toppingAsAString);


            //Use If Else statement to set the variable value for ToppingCost.
            if (topping == 1) 
            {
                ToppingCost = 2.0;
                ToppingName = ("Pepperoni");
            }
            else if (topping == 2)
            {
                ToppingCost = 2.0;
                ToppingName = ("Ham");
            }
            else if (topping == 3)
            {
                ToppingCost = 1.0;
                ToppingName = ("Onions");
            }
            else if (topping == 4)
            {
                ToppingCost = 1.0;
                ToppingName = ("Mushrooms");
            }
            else if (topping == 5)
            {
                ToppingCost = 1.0;
                ToppingName = "Green Peppers";
            }

             TotalCost = (SizeCost + ToppingCost);
            Tax = (TotalCost*0.085);
            TotalWithTax = (TotalCost + Tax);

            //Display order details.
            Console.WriteLine("Here are the details for your order.");
            Console.WriteLine("Thank you for your business!");
            Console.WriteLine("You can pick up your pizza in 25 minutes!");

            //Show current time of order.
            DateTime now = DateTime.Now;
            Console.WriteLine("Time Ordered: "+now+" ");

            //Show Current time of order with additional 25 minutes for pickup.
            DateTime pickup = DateTime.Now.AddMinutes(25);
            Console.WriteLine("Pick Up At: "+pickup+" ");

            //Output Pizza Size.
            Console.WriteLine("Size: " +SizeName+ "  ");

            //OutPut Topping name.
            Console.WriteLine("Topping: " +ToppingName+ " ");

            Console.WriteLine("---------------");

            //Output total price of size and topping chosen.
            Console.WriteLine("Pizza Price: $ "+TotalCost+" ");

            //Output tax amount.
            Console.WriteLine("Tax: $" +Tax+ " ");

            //Output total price with tax.
            Console.WriteLine("Total Price: $" +TotalWithTax+ "  ");


        }
        else 
        {
            //Exit the program because the customer does not want to order a pizza.
            Console.WriteLine("Alright, have a great day!");
        }

        Console.ReadLine();


    }
}

提琴手Demo