为什么我的While循环不会循环?

时间:2012-11-19 12:29:19

标签: c# while-loop console-application break nested-loops

好吧,我从我能想到的每一个角度都尝试过这个,而且我认为我按照惯例过度复杂化了!

我正在构建一个控制台c#应用程序,它将要求用户输入所购买商品的价格,然后根据此客户代码输入“客户代码”,将应用某个折扣。

我为此使用了一个switch语句,它都与错误检查一起工作(使用while循环继续询问,直到识别出正确的输入)这是最后一部分我正在努力...控制台询问用户是否他们想要输入更多数据(跳回主循环的开头)如果用户输入了错误的输入,如果用户输入'N'程序终止,它也会根据需要再次询问。但是不起作用的是如果用户输入“Y”,他们应该能够返回到开始并输入更多数据,但这不起作用= /我使用了“中断”;声明打破退出循环并返回主循环...

此时字符为'Y',因此主循环应该仍然在运行,但是控制台什么都不做,只是将光标放在一个空白行上......它要求没有输入而且它没有说“按”任何继续的关键“。我已经尽可能多地详细说明了,我很抱歉这篇文章= / ......下面是我的代码......希望有人能发现我哪里错了!

更新:我还应该注意到我已经尝试了主循环(=='Y')并将变量设置为'Y',以便它执行第一个循环。我还将它改为使用相同语句的do while循环,因此首先使用空白字符运行,然后如果将其更改为'Y',则循环条件应该已被排除= /

更新:我已经注意到我的计算错误之前你们都认为我更像是一个白痴= / LOL

namespace W7Task1
{
    class W7Task1
{
    // "Main" method begins the execution of the C# application
    static void Main(string[] args)
    {
        char customerCode = '\0';
        double initialCost = 0;
        string customerType = "";
        double finalPrice = 0;
        string userInput = "";
        char continueChar = '\0';

        while (continueChar != 'N')
        {
            while (initialCost == 0)
            {
                Console.Write("\nPlease input the cost of the item: ");
                userInput = Convert.ToString(Console.ReadLine());
                try
                {
                    initialCost = Convert.ToDouble(userInput);
                }
                catch
                {
                    Console.WriteLine("\nPlease input a number!");
                }
            }

            while (customerCode == '\0')
            {
                Console.Write("\nPlease input your customer code: ");
                userInput = Convert.ToString(Console.ReadLine());
                customerCode = Convert.ToChar(userInput);
                customerCode = char.ToUpper(customerCode);

                switch (customerCode)
                {
                    case 'A':
                        customerType = "Managerial Staff";
                        finalPrice = (initialCost / 100) * 30 - initialCost;
                        Console.WriteLine("\nThe initial cost of the item is: {0:c}\nYour customer type is: {1}\nThe items final price is: {2:c}\n", initialCost, customerType, finalPrice);
                        break;
                    case 'B':
                        customerType = "Sales Staff";
                        finalPrice = (initialCost / 100) * 20 - initialCost;
                        Console.WriteLine("\nThe initial cost of the item is: {0:c}\nYour customer type is: {1}\nThe items final price is: {2:c}\n", initialCost, customerType, finalPrice);
                        break;
                    case 'C':
                        customerType = "Account Customers";
                        finalPrice = (initialCost / 100) * 8 - initialCost;
                        Console.WriteLine("\nThe initial cost of the item is: {0:c}\nYour customer type is: {1}\nThe items final price is: {2:c}\n", initialCost, customerType, finalPrice);
                        break;
                    case 'D':
                        customerType = "Cash Customers";
                        finalPrice = (initialCost / 100) * 5 - initialCost;
                        Console.WriteLine("\nThe initial cost of the item is: {0:c}\nYour customer type is: {1}\nThe items final price is: {2:c}\n", initialCost, customerType, finalPrice);
                        break;
                    case 'E':
                        customerType = "Credit Card/Cheque";
                        finalPrice = (initialCost / 100) * 0 - initialCost;
                        Console.WriteLine("\nThe initial cost of the item is: {0:c}\nYour customer type is: {1}\nThe items final price is: {2:c}\n", initialCost, customerType, finalPrice);
                        break;
                    default:
                        Console.WriteLine("\nError Please input a valid Customer Code\n");
                        customerCode = '\0';
                        break;
                }
            }

            while (continueChar == '\0')
            {
                Console.WriteLine("Would you like to input more data?");
                userInput = Convert.ToString(Console.ReadLine());

                if (char.TryParse(userInput, out continueChar))
                {
                    continueChar = char.ToUpper(continueChar);

                    if (continueChar == 'Y')
                    {
                        break;
                    }
                    else if (continueChar == 'N')
                    {
                        Console.WriteLine("Thankyou for using this application");
                        System.Environment.Exit(0);
                    }
                    else
                    {
                        Console.WriteLine("Please input a 'Y' or 'N'");
                        continueChar = '\0';
                    }
                }
                else
                {
                    Console.WriteLine("Please input a valid character!");
                }
            }
        }
    }// End of "Main" method
}// End of "W7Task1" class

}

3 个答案:

答案 0 :(得分:3)

您没有重置变量。

当用户退出退出循环时,变量如下

continueChar == 'Y'
customerCode != '\0'
initialCost != 0

这意味着所有的while循环都不会触发。

在主循环中移动变量声明或初始化。

答案 1 :(得分:3)

发生的事情是,在用户输入'Y'后,外部循环(即while (continueChar != 'N'))将继续无限循环,但没有内部循环(从while (initialCost == 0)开始)将满足他们的条件,因为他们的变量(例如initialCost)将保留在前一次迭代中分配的值。

最简单的解决方法是将所有变量初始化移到外部循环中。更改您的代码:

static void Main(string[] args)
{
    char customerCode = '\0';
    double initialCost = 0;
    string customerType = "";
    double finalPrice = 0;
    string userInput = "";
    char continueChar = '\0';

    while (continueChar != 'N')
    {
        while (initialCost == 0)
        {
        // ...

...为:

static void Main(string[] args)
{
    char continueChar = '\0';

    while (continueChar != 'N')
    {
        char customerCode = '\0';
        double initialCost = 0;
        string customerType = "";
        double finalPrice = 0;
        string userInput = "";

        while (initialCost == 0)
        {
        // ...

编辑:如果要在方法的顶部保留变量声明,可以将声明和初始化分开。这将确保在内循环的每次迭代中重置它们,同时保持您的导师满意。

static void Main(string[] args)
{
    char customerCode;
    double initialCost;
    string customerType;
    double finalPrice;
    string userInput;
    char continueChar = '\0';

    while (continueChar != 'N')
    {
        customerCode = '\0';
        initialCost = 0;
        customerType = "";
        finalPrice = 0;
        userInput = "";

        while (initialCost == 0)
        {
        // ...

答案 2 :(得分:0)

您是否使用调试器逐步完成了应用程序(在Visual Studio中按'F10')?

您没有将'initialCost'变量重置为0.