开发者

celsius to fahrenheit

This is what i have i need for it to run and ask for celsius instead of fahrenheit but it is not doing it..

Dim celsius, fahrenheit As Double
    Console.WriteLine("Please enter a celsius temperature:")
    celsius = Convert.ToDouble(Console.ReadLine())
    fahrenheit = celsius * 9 / 5 + 32
    celsius = Math.Round(fahrenheit, 1)
    Console.WriteLine(celsius & " C =" & fahrenheit & " F")

I am not understand this at all i am suppose also ask users to inpu开发者_如何学Ct a celsius degree for this part.its telling me i nedd to change formula to calculate the fahrenheit temp for this part.


Dim celsius, fahrenheit As Double
    Console.WriteLine("Please enter a celsius temperature:")
    celsius = Convert.ToDouble(Console.ReadLine())
    fahrenheit = celsius * 9 / 5 + 32

fahrenheit = Math.Round(fahrenheit, 1)

    Console.WriteLine(celsius & " C =" & fahrenheit & " F")


You are assignment is wrong here - celsius = Math.Round(fahrenheit, 1) should be assigned to fahrenheit ..Formulas are -

c=(5/9) * (fahrenheit - 32)
f=(9/5) * celsius  + 32

Corrected code -

  Dim celsius, fahrenheit As Double
        Console.WriteLine("Please enter a celsius temperature:")
        celsius = Convert.ToDouble(Console.ReadLine())
        fahrenheit = celsius * 9 / 5 + 32
        fahrenheit = Math.Round(fahrenheit, 1)//assign fahrenheit 
        Console.WriteLine(" C =" & celsius & " F=" & fahrenheit)


How about this:

    Dim celsius, fahrenheit As Double
    Console.WriteLine("Please enter a celsius temperature:")
    If Double.TryParse(Console.ReadLine(), celsius) Then 'check input
        fahrenheit = celsius * 9.0R / 5.0R + 32.0R  'convert it
        Console.WriteLine("{0:F1}°C = {1:F1}°F", celsius, fahrenheit) 'show it
    End If

so you store the full precision number, but only display it with 1 decimal. Also using the TryParse() function is much safer as it does not throw an Exception if the input is not a number. Alternatively you can use Conversion.Val() which is much more robust than Convert.ToDouble().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜