C#温度转换(1期)

时间:2014-02-14 14:17:36

标签: c#

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

 namespace ConTempConversion_Michelle
 {
    class Program
    {
       static void GetChoice(ref int ichoice)
       {
            Console.Write("Enter choice: ");
            string input = Console.ReadLine();// get string from the user

              bool result = int.TryParse(input, out ichoice);//convert input from     string to ichoice int value

          if (!result)
          {
             while (!result && ichoice > 3)
             {
                Console.WriteLine("Invalid value.Try again:");

                input = Console.ReadLine();

                result = int.TryParse(input, out ichoice);
             }
          }
    }

    static void Main(string[] args)
    {
        double fTemp;
        double cTemp;
        double convertToC;
        double convertToF;

        Console.WriteLine("Welcome to the temperature conversion application");
        Console.WriteLine("_________________________________________________");
        Console.WriteLine("1. Fahrenheit to Celsius");
        Console.WriteLine("2. Celsius to Fahrenheit");
        Console.WriteLine("3. Exit");

        int ichoice = 0;
        GetChoice(ref ichoice);
        do
        {

            if (ichoice == 1)
            {
                Console.WriteLine("Enter Fahrenheit temperature: ");
                fTemp = int.Parse(Console.ReadLine());
                convertToC = ConvertCelcius(fTemp);
                Console.WriteLine(fTemp + "Fahrenheit is " + convertToC + "Celsius");
                Console.WriteLine("Welcome to the temperature conversion application");
                Console.WriteLine("_________________________________________________");
                GetChoice(ref ichoice);
            }
            if (ichoice == 2)
            {
                Console.WriteLine("Enter Celsius temperature: ");
                cTemp = int.Parse(Console.ReadLine());
                convertToF = ConvertCelcius(cTemp);
                Console.WriteLine(cTemp + "Celsius is " + convertToF+ "Fahrenheit");
                Console.WriteLine("Welcome to the temperature conversion application");
                   Console.WriteLine("____________________________________________________");
                GetChoice(ref ichoice);

            }
            if (ichoice == 3)
            {
                Console.WriteLine("Thank you for using the temperature conversion application. Please come again.");
            }
            else
            {
                Console.WriteLine("Invalid choice. Please choose again!");
            }

        }
        while (ichoice < 3);
        Console.ReadKey();

    }
    static double ConvertCelcius(double c)
    {
        double f;

        return f = 9.0 / 5.0*c + 32;
    }
    static double ConvertFahrenheit(double f)
    {
        double c;

        return c = (f  -  32)  *  (5.0/9.0);

       }
    }
 }

我这个程序只有一个输出问题。从C到F的转换是正确的,但从F到C是不正确的。数学公式是正确的,但为什么输出不是?请帮忙......谢谢。

米歇尔

1 个答案:

答案 0 :(得分:7)

你说错了方法:

        if (ichoice == 2)
        {
            Console.WriteLine("Enter Celsius temperature: ");
            cTemp = int.Parse(Console.ReadLine());
            convertToF = **ConvertCelcius**(cTemp);
            Console.WriteLine(cTemp + "Celsius is " + convertToF+ "Fahrenheit");
            Console.WriteLine("Welcome to the temperature conversion application");
               Console.WriteLine("____________________________________________________");
            GetChoice(ref ichoice);

        }