简体   繁体   English

使用TryParse C#检查负值

[英]Check for negative value with TryParse C#

i know there are several threads about this topic, but I didn't find an answer for me. 我知道关于此主题有多个主题,但我没有找到答案。 So I check for correct values with double.TryParse , but I also have to check if they're negative: 因此,我使用double.TryParse检查正确的值,但我还必须检查它们是否为负值:

static void Main(string[] args)
{
    double radius, hoehe, umfang, volumen, oberfl;

    Console.WriteLine("Radius des Zylinders eingeben: ");

    while (!double.TryParse(Console.ReadLine(), out radius))
    {
        Console.WriteLine("Wert ist ungültig! Bitte erneut versuchen.");
    }

    Console.WriteLine("Hoehe des Zylinders eingeben: ");

    while (!double.TryParse(Console.ReadLine(), out hoehe))
    {
        Console.WriteLine("Wert ist ungültig! Bitte erneut versuchen.");
    }

    umfang = 2 * Math.PI * radius;
    volumen = Math.PI * Math.Pow(radius, 2) * hoehe;
    oberfl = umfang * (radius + hoehe);

    Console.WriteLine("Umfang: {0:f2}", umfang);
    Console.WriteLine("Volumen: {0:f2}", volumen);
    Console.WriteLine("Oberfläche: {0:f2}", oberfl);

    Console.ReadKey();
}

First I thought just add it to the while statement, like 首先,我认为只需将其添加到while语句中,例如

while (!double.TryParse(Console.ReadLine(), out radius) && radius <=0)

but this doesn't work. 但这不起作用。 So can you please tell me another possibility, maybe another if statement? 那么,能否请您告诉我另一种可能性,也许是另一种if陈述?

Change && to || &&更改为|| . As || 作为|| stops after first expression to be evaluated as true then: 在第一个表达式被评估为true后停止,然后:

  • If the input is not a double then it will iterate again (and won't check second condition as && would - which was the problem) 如果输入不是双精度数,则它将再次进行迭代(并且不会像&&那样检查第二个条件-这就是问题所在)
  • If the input is a double but is <= 0 it will iterate again. 如果输入为double但<= 0 ,它将再次迭代。

Therefore it will stop iterating iff input is a positive double. 因此,如果输入为正双精度,它将停止迭代。

while (!double.TryParse(Console.ReadLine(), out radius) || radius <=0)

Also as you are using this code twice (at least) maybe encapsulate it in a function? 另外,由于您两次(至少)使用此代码,可能将其封装在一个函数中?

public double GetPositiveDouble()
{
    double result;
    while (!double.TryParse(Console.ReadLine(), out result))
    {
        Console.WriteLine("Wert ist ungültig! Bitte erneut versuchen.");
    }
    return result;
}

You can use an overload of double.TryParse that accept NumberStyles flags. 可以使用接受NumberStyles标志的double.TryParse重载。 If you expect a floating point number without a sign (eg only non-negative) you can specify that: 如果期望的浮点数没有符号(例如,只有非负数),则可以指定:

double.TryParse(
    Console.ReadLine(),
    NumberStyles.AllowDecimalPoint,
    CultureInfo.CurrentCulture,
    out var radius)

This will return false if the number has a sign (both - or + ) and you know that the specified number is always non-negative. 如果该数字带有符号( -+ )并且您知道指定的数字始终为非负数,则将返回false。

Notice that this return true if the number 0.0 which is not exactly what you want. 请注意,如果数字0.0并非您真正想要的,则返回true。

If you also want to allow whitespace and exponential notation etc. you have to provide additional NumberStyles flags. 如果您还希望允许使用空格和指数表示法等,则必须提供其他NumberStyles标志。

You condition is wrong. 您的条件是错误的。 It should read: 它应显示为:

while (!(double.TryParse(Console.ReadLine(), out radius) && radius > 0))

您的代码可以工作...应该是OR,而不是AND:

while (!double.TryParse(Console.ReadLine(), out radius) || radius <=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM