如何将空值转换为默认值(浮点数)或0

问题描述:

我想获得浮点值作为输入,如果一个值被跳过,只是将其设置为0,但由于跳过将其设置为空我得到一个异常。所以,我尝试使用“??”操作是这样的:如何将空值转换为默认值(浮点数)或0

float speed = Convert.ToSingle(Console.ReadLine()) ?? default(float); 

float speed = Convert.ToSingle(Console.ReadLine()) ?? 0; 

但我得到一个错误:

错误CS0019:操作 '?'不能应用于类型 “浮动”和“廉政”

的操作数我怎么会写这样的等价物:float y = x ?? 0;

public static void CalculateDistanceTimeSpeed() 
{ 
    Console.WriteLine("Find distance/speed/time based on any of the two."); 
    Console.WriteLine("Enter two of the three requirements"); 
    Console.WriteLine(); 

    Console.WriteLine("Enter speed in km/h (Enter 0 if unkown)"); 
    float speed = Convert.ToSingle(Console.ReadLine()); 

    Console.WriteLine("Enter distance in km (Enter 0 if unkown)"); 
    float distance = Convert.ToSingle(Console.ReadLine()); 

    Console.WriteLine("Enter time in hour/s (Enter 0 if unkown)"); 
    float time = Convert.ToSingle(Console.ReadLine()); 

    Console.WriteLine(distance, speed, time); 

    if (speed == 0) 
    { 
     speed = distance/time; 
     speed.ToString("0.##"); 

     Console.WriteLine("Determined Average speed : {1}km/h, based on distance: {0}km and time: {2}hrs.", distance, speed, time); 
    } 

    else if (distance == 0) 
    { 
     distance = time/speed; 
     distance.ToString("0.##"); 

     Console.WriteLine("Determined distance traveled : {0}km, based on speed: {1}km/h and time: {2}hrs.", distance, speed, time); 

    } 

    else 
    { 
     time = distance/speed; 
     time.ToString("0.##"); 

     Console.WriteLine("Determined time traveled: {2}hrs, based on speed: {1}km/h for distance: {0}km.", distance, speed, time); 
    } 
+0

我在这里的任何地方都看不到'null'。 –

+0

使用float.TryParse。 – mybirthname

+0

首先搜索为什么你得到这个错误?由于数据类型不可为空。所以你可以检查如何使它为空。 –

您需要使用0f告诉编译器你0一个float而不是一个int

有关详细信息,请参阅MSDN

+0

但他不能在值类型上使用空合并运算符,只能在空值(引用类型)的类型上使用。 –

+0

他的问题是“我怎么写这个相当于:float y = x ?? 0;” – toadflakz

你不能。 由于Convert.ToSingle()返回float而不是float?结果永远不会null

Convert.ToSingle()如果格式未知,将会抛出异常,它永远不会返回null。您应该使用Single.TryParse()来轻松检查结果是否正确。

float speed; 
if (!Single.TryParse(Console.ReadLine(), out speed)) 
    speed = 0f; 

另请注意,这些方法将使用当前语言环境,以便他们可能在不同语言环境中接受不同的输入。请确保您指定正确的语言环境,如果输入的预期有一定的语言环境(例如小数点分隔符)

+0

工作。谢谢。 – Buriz

不能使用值类型的空合并运算符(除nullable types),并参照类型,可以是null。但是,您可以使用此一班轮:

float speed = Single.TryParse(Console.ReadLine(), out speed) ? speed : default(float); 
+0

这不是事实,你可以使用null合并运算符和可为空值的类型,它们也是值类型。 – Sean

已经有发布有效的答案,但我猜它是适当给予一些背景为什么这个错误error CS0019: Operator ??' cannot be applied to operands of type 'float' and 'int'出现。

在C#中我们有引用类型和值类型。区别在于

引用类型的变量存储对其数据(对象)的引用,而值类型的变量直接包含其数据。

如在MSDN中所述。

由于floatinta couple more类型实际上是值类型,为什么你不能对这些类型的使用null-coalescing operator它变得很明显。他们根本不可能是null,因为他们没有存储任何参考(可能是null),但它本身就是价值。

只是为了回答你的问题:

我怎么会写这样的等价的:漂浮Y = X? 0

float? x = 10f; 
float y = x ?? 0f; 

这工作。多谢你们。这是为我工作的解决方案,以防万一有人好奇。

public static void CalculateDistanceTimeSpeed() 
{ 
    float speed; 
    float distance; 
    float time; 

    Console.WriteLine("Find distance/speed/time based on any of the two."); 
    Console.WriteLine("Enter two of the three requirements"); 
    Console.WriteLine(); 

    Console.WriteLine("Enter speed in km/h"); 
    if (!Single.TryParse(Console.ReadLine(), out speed)) 
     speed = 0f; 

    Console.WriteLine("Enter distance in km"); 
    if (!Single.TryParse(Console.ReadLine(), out distance)) 
     distance = 0f; 

    Console.WriteLine("Enter time in hour/s"); 
    if (!Single.TryParse(Console.ReadLine(), out time)) 
     time = 0f; 

    if (speed == 0f) 
    { 
     speed = distance/time; 
     Console.WriteLine("Required average Speed : {1}km/h for distance {0}km and time {2}hrs.", distance, speed.ToString("0.##"), time); 
    } 

    else if (distance == 0f) 
    { 
     distance = speed * time; 
     Console.WriteLine("Distance traveled : {0}km at speed {1}km/h for {2}hrs.", distance.ToString("0.##"), speed, time); 

    } 

    else 
    { 
     time = distance/speed; 
     Console.WriteLine("Travel time: {2}hrs with speed {1}km/h for distance {0}km.", distance, speed, time.ToString("0.##")); 
    } 

    // add mph conversion 
}