4

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

我正在构建一个控制台 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

}

4

3 回答 3

3

你没有重置你的变量。

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

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

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

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

于 2012-11-19T12:36:01.553 回答
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)
        {
        // ...
于 2012-11-19T12:36:33.147 回答
0

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

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

于 2012-11-19T12:37:16.793 回答