实现用户在转换为摄氏度或华氏度之间选择的最佳方式是什么?

问题描述 投票:1回答:1
namespace TemperatureConverter
{
    class Program
{
    public static float celsius, fahrenheit;

    public static void Main(string[] args)
    {
        Console.Write("Are you converting Celsius or Fahrenheit?: ");
        celsius = Convert.ToSingle(Console.ReadLine());

        fahrenheit = (celsius * 9 / 5) + 32;

        Console.WriteLine("The temperature in fahrenheit is " + fahrenheit);
    }

    static void ToFahrenheit()
    {

    }

    static void ToCelsius()
    {

    }
}

}

我试图实现一种方式,让用户在编译后可以选择转换为摄氏度或华氏度。

c# user-input
1个回答
2
投票
Console.WriteLine("Do you want to convert to (F)ahrenheit or to (C)elsius? ");
ConsoleKeyInfo key;
do
{
    key = Console.ReadKey();
    if (key.Key == ConsoleKey.F)
    {
        return ToFahrenheit(/* */);
    }
    if (key.Key == ConsoleKey.C)
    {
        return ToCeclsius(/* */);
    }
    Console.WriteLine("Please press F or C to make your selection");
} while (true);
© www.soinside.com 2019 - 2024. All rights reserved.