【问题标题】:Why won't my While loop, loop?为什么我的 While 不会循环,循环?
【发布时间】:2012-11-07 09:04:44
【问题描述】:

好的,我从我能想到的各个角度都试过了,我想我像往常一样把事情复杂化了!

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

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

此时字符是'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

}

【问题讨论】:

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


    【解决方案1】:

    实际情况是,用户输入'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)
            {
            // ...
    

    【讨论】:

    • 我知道这很简单!我刚吃了一个“哦,是的……呃!”时刻。非常感谢=]
    • 第一眼我也错过了。但是当我单步执行代码时,它变得很明显。尝试学习如何使用 Visual Studio 调试器;它在这些情况下大有帮助。
    • 我是一年级学生,他们只告诉我们“无需调试即可运行”=/ 我个人看到的唯一区别是,当没有更多代码时,调试将关闭控制台被执行并且没有调试将保持窗口打开,要求您手动退出。
    • 我们还被要求在方法的顶部声明所有变量,否则我会错过演示分数 =/ 我明天将不得不问我的导师,如果我将它稍微向下移动会失去分数因为它不像我把它们都分开了,它有助于功能
    • 如果您在代码中设置断点,您将看到两种模式之间的区别。我的代码中的最后一次编辑应该满足您导师的要求。
    【解决方案2】:

    您是否使用调试器单步执行您的应用程序(在 Visual Studio 中按“F10”)?

    您没有将“initialCost”变量重置为 0。

    【讨论】:

    • 我也在做“黑盒测试”(我对此感到困惑,所以我刚刚列出了每次测试中发生的所有事情,当我理解时我可以将它安排成黑盒格式. 感谢您的回复
    【解决方案3】:

    你没有重置你的变量。

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

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

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

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

    【讨论】:

    • 我会复制并粘贴我对上述答案的评论,但这会很粗鲁,所以也谢谢你的回复=]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    • 2016-01-19
    • 1970-01-01
    相关资源
    最近更新 更多