【问题标题】:Do while loop is moving to the next step without meeting conditions在不满足条件的情况下做while循环进入下一步
【发布时间】:2018-01-17 07:46:58
【问题描述】:

我这周刚开始学习 C#,我正在尝试运行一个简单的代码,如果用户输入文本,则提示输入数字,如果输入负数,则提示用户输入正数(所以文本的布尔运算和否定的 if 语句)。如果他们输入有效(正)数,程序将继续执行其余步骤。

但是使用此代码,如果用户输入一个负数,然后是一个文本,然后是另一个负数等等,它似乎会中断循环并继续执行下一个操作。

代码是一个更大程序的一部分,因此我将其缩小并仅提取最关键的部分以使其运行。有人能发现我在这里错过了什么吗?

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

namespace IncomeTaxCalculator
{
    class IncomeTax
    {
        public static void Main()
        {
            double income;
            income = income_input();
            show_output(income);
        }
        public static double income_input()
        {
            double income; string income_string; bool bad_value = true;
            do
            {
                Console.Write("What is your total income: ");
                income_string = Console.ReadLine();
                if (double.TryParse(income_string, out income))
                {
                    bad_value = false;
                }
                else
                {
                    Console.WriteLine("Enter your income as a whole-dollar numeric figure.");
                }
                if (income < 0)
                {
                    Console.WriteLine("Your income cannot be a negative");
                }
            } while (bad_value || income < 0);
            return income;
        }
              public static void show_output(double income)
        {
            Console.WriteLine("Your income is " + income);
            Console.WriteLine("\n\n Hit Enter to exit.");
            Console.ReadLine();
        }
    }
}

【问题讨论】:

  • 我已经可以告诉您,您在标题中所做的假设是错误的。也许调试它以查看值:)
  • 在 C# 中,命名方法的标准是 PascalCaseIncomeInput 而不是 income_input),对于局部变量,它是 camelCaseincomeString 而不是 income_string
  • 你需要在循环开始时将bad_value重新初始化为true。
  • 为帮助的家伙干杯

标签: c# do-while


【解决方案1】:

我意识到这已经被接受了,但这可以在一个更简单的循环中完成。为什么不直接创建一个无限循环和break/return 当值满足时。而不是检查无效输入搜索有效输入。

我不会详细说明为什么这是一个更可接受的解决方案,请考虑给出的说明,如果您预期输入无效,那么您的说明是错误的。而是检查积极的结果。 Read This!!

static double income_input()
{
    double income = double.NaN;
    while (true)
    {
        Console.WriteLine("What is your income?:");
        if (double.TryParse(Console.ReadLine(), out income) && income > 0)
            return income;
        Console.WriteLine("Invalid input. Please enter a valid number greater than zero.");
    }
}

实际上,我们在这里所做的只是用while(true) 创建了一个。所以现在循环永远不会结束,除非我们明确告诉它。

接下来您可以简单地解析结果并确保double.TryParse 成功和income &gt; 0 的条件。注意 return 只是退出循环。

现在编译(注意最后没有 return),因为编译器知道唯一的退出点是通过 return 语句。 Example Post

如果您想获得尽可能短的代码,可以对inline variables 使用一些 C# 7 语法。

static double income_input()
{
    while (true)
    {
        Console.WriteLine("What is your income?:");
        if (double.TryParse(Console.ReadLine(), out double income) && income > 0)
            return income;
        Console.WriteLine("Invalid input. Please enter a valid number greater than zero.");
    }
}

编码愉快!

【讨论】:

    【解决方案2】:

    这就是正在发生的事情。当您输入负数时,bad_value 将被设置为 false。那么当你输入一个非数字值时income会被TryParse设置为0。现在你bad_value || income &lt; 0 的条件是假的。要修复它,您只需在每个循环开始时将 bad_value 重置为 true。

    或者,您可以按照 René Vogt 的建议在 else 中将 bad_value 设置为 true,另外在检查是否为负的 if 中设置为 true,然后您可以执行 while(bad_value)

    do
    {
        Console.Write("What is your total income: ");
        income_string = Console.ReadLine();
        if (double.TryParse(income_string, out income))
        {
            bad_value = false;
        }
        else
        {
            Console.WriteLine("Enter your income as a whole-dollar numeric figure.");
            bad_value = true;
        }
        if (income < 0)
        {
            Console.WriteLine("Your income cannot be a negative");
            bad_value = true;
        }
    } while (bad_value);
    

    【讨论】:

    • 或在else 部分中将bad_value 设置为true
    • 完美。谢谢
    【解决方案3】:

    把你的代码改成这样:

     double income;
     string income_string;
     do
     {
            Console.Write("What is your total income: ");
            income_string = Console.ReadLine();
     } while (!double.TryParse(income_string, out income) || income < 0);
    //rest of your code here, in another method that takes the valid income
    

    您应该将获得收入的方法与其中包含(业务)逻辑的方法分开。

    【讨论】:

    • 如果您输入不可解析的文本,循环将结束。
    • 你的意思是while(!double.TryParse(income_string, out income) || income &lt; 0)
    • 另外这不包括现在的两个具体错误信息。
    猜你喜欢
    • 1970-01-01
    • 2021-12-14
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多