根据文本或数字输入显示C#错误消息

时间:2016-08-17 14:39:20

标签: c# if-statement

我尝试写行,如果输入了文本(数字以外的任何内容),则会显示错误消息,表明它不是数字,并要求用户再次输入。

此外,我已植入该号码必须为20或更高才能正常工作,当用户输入少于20时,它会显示错误消息并再次询问用户..

所以我的问题是它显示包含少于20的数字和文本的所有内容的错误消息。

那么,如果文本是类型而不是数字,我如何制作显示不同消息的else语句?

static double InputFuel() {
    double fFuel;
    string text;
    bool badValue = true;

    Console.Write("Enter amount of fuel used in litres : ");
    //Check if fule entered is greater than 20, if not ask again
    do {

        text = Console.ReadLine();
        if (double.TryParse(text, out fFuel) && fFuel >= 20) {
            badValue = false;
        }

        else {
            Console.WriteLine("\n\t {0} is below the minimum value of 20  \n\n", text);
            Console.Write("Please re-enter a number greater than 20 : ");
        }
    } while (badValue);
    return fFuel;
}//end InputFuel

我尝试了类似的东西,但没有工作

        else (!int.TryParse(text, out num) {
            Console.WriteLine("\n\t {0} is not a number  \n\n", text);
        }

3 个答案:

答案 0 :(得分:3)

首先,我会检查输入是否为数字,然后检查数字是否大于20

static double InputFuel() {
    double fFuel;
    string text;
    bool badValue = true;

    Console.Write("Enter amount of fuel used in litres : ");
    //Check if fule entered is greater than 20, if not ask again
    do {

        text = Console.ReadLine();
    if (!double.TryParse(text, out fFuel) {
            Console.WriteLine("\n\t {0} is not a number  \n\n", text);
        }

        else if (fFuel >= 20) {
            badValue = false;
        }

        else {
            Console.WriteLine("\n\t {0} is below the minimum value of 20  \n\n", text);
            Console.Write("Please re-enter a number greater than 20 : ");
        }
    } while (badValue);
    return fFuel;
}//end InputFuel

答案 1 :(得分:0)

您可以将当前条件修改为嵌套,如下所示

    if (double.TryParse(text, out fFuel)) {
      if(fFuel >= 20) badValue = false;
      else {
        Console.WriteLine("\n\t {0} is below the minimum value of 20  \n\n", text); 
        Console.Write("Please re-enter a number greater than 20 : ");
      }
    }
    else {
      Console.WriteLine("\n\t {0} is not a number  \n\n", text);
    }

答案 2 :(得分:0)

如果您正在寻找更高级的'或者更像企业的'验证用户输入的方法您可以创建一个将进行验证的结构或类,并提供有关它的一些信息:

class InputValidator
{
    public string Input { get; set; }
    public bool IsValidInput { get; set; }
    public bool IsAboveThreshHold { get; set; }
    public bool IsNumber { get; set; }
    public double Litres { get; set; }

    public ValidationResult() { }

    public ValidationResult(string text)
    {
        double litres; Input = text;
        if (double.TryParse(text, out litres))
        {
            Litres = litres;
            IsAboveThreshHold = litres > 20;
            IsNumber = true;                
        }

        IsValidInput = IsNumber && IsAboveThreshHold;
    }

    public void ShowErrorMessage()
    {
        if (!IsNumber)
        {
            Console.WriteLine($"\n\t {Input} is not a valid number \n\n");
            Console.Write("Please re-enter a number greater than 20 : ");                
            return;
        }
        if(!IsAboveThreshHold)
        {
            Console.WriteLine($"\n\t {Input} is below the minimum value of 20  \n\n");
            Console.Write("Please re-enter a number greater than 20 : ");
        }
    }
}

非常容易使用这个课程:

    static double InputFuel()
    {
        var result = new InputValidator();

        Console.Write("Enter amount of fuel used in litres : ");

        //Check if fule entered is greater than 20, if not ask again
        while (!result.IsValidInput)
        {
            result = new InputValidator(Console.ReadLine());

            if (!result.IsValidInput) result.ShowErrorMessage();                    
        }
        return result.Litres;
    }

P.S。 虽然在简单的情况下,这将是一种矫枉过正,但在企业项目中常见的更复杂的情况下,这种方法更好用。