简体   繁体   中英

temperature conversion using functions c#

hello i have looked around and couldnt find ac# console application that uses functions to convert a temperature. i have almost completed the program but for some reason the celcius temperature does not show up if someone could help lead me to the answer that would be great!

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

namespace temp_conversion
{
class tempConversion
{
    static void Main(string[] args)
    {
        double far, cel;
        far = GetTemp("Far");
       cel= Celcius(far);
        DisplayResults(far,cel);


    }//end of main method

    public static double GetTemp(string temp)
    {
        string inputValue;
        double far;
        Console.WriteLine("Enter Fahrenheith Temp");
        inputValue = Console.ReadLine();
        far = double.Parse(inputValue);
        return far;
    }

    static double Celcius(double far)
    {

        double cel = 5.0 / 9.0 * (far - 32);
        return cel;

    }

    public static void DisplayResults (double far , double cel)
    {
        Console.WriteLine("Fahrenhieith temp {0:N2}", far);
        Console.WriteLine("C    ", cel);
        Console.ReadLine();
        return;
    }
}//end of class
}

You need to include the {0:N2} like you have for far in the writeline for cel.

eg

Console.WriteLine("Celcius Temp {0:N2}", cel);

You should replace

Console.WriteLine("C    ", cel);

with

Console.WriteLine("C   {0:N2} ", cel);

You can also try this

Console.WriteLine("C {0:0.00}", cel);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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