Celsius到fahrenheit计算器控制台应用程序C#

本文关键字:应用程序 控制台 计算器 fahrenheit Celsius | 更新日期: 2023-09-27 18:22:20

我已经为我的摄氏到华氏转换器写了这段代码,反之亦然。一切似乎都很顺利。唯一不太好的是,当用户获得转换后的温度时,文本颜色应该根据温度而变化。我也会在下面发布我的代码,有人能告诉我是否做错了什么,以及我必须做些什么来修复它吗谢谢你我真的很感激

static void Main(string[] args)
    {
        // Program that converts Celsius to Fahrenheit and vice versa

        int Celsius, Fahrenheit, UserChoice;
        //Console background colour
        Console.BackgroundColor = ConsoleColor.DarkMagenta;
        Console.Clear();
        Console.SetCursorPosition(15, 0);
        Console.WriteLine("Welcome to the eBSolutions Temperature Converter By Y. Ibrahim");
        Console.WriteLine("");
        Console.WriteLine("");
        Console.WriteLine("<<<<<<<<<<<< Press Enter to continue to the Main Menu >>>>>>>>>>>>");
        Console.ReadLine();
        Console.Clear();

        Console.SetCursorPosition(15, 0);
        Console.WriteLine("Main Menu");
        Console.SetCursorPosition(0, 4);
        Console.WriteLine("1) Convert Celsius to Fahrenheit");
        Console.WriteLine("2) Convert Fahrenheit to Celsius");
        Console.WriteLine("3) Exit ");
        Console.WriteLine("4) Help ");

        Console.SetCursorPosition(0, 9);
        Console.WriteLine("Please Enter one of the provided options from above");
        UserChoice = Convert.ToInt16(Console.ReadLine());
        Console.Clear();

        // Convert Celsius to Fahrenheit.
        if (UserChoice == 1)
        {
            Console.SetCursorPosition(20, 0);
            Console.WriteLine("Converting Celsius To Fahrenheit");
            Console.SetCursorPosition(0, 4);
            Console.WriteLine("Enter the Temperature in Celsius(°C) : ");
            Celsius = int.Parse(Console.ReadLine());
            Fahrenheit = (Celsius * 9) / 5 + 32;
            Console.WriteLine("The temperature in Fahrenheit is(°F) : " + Fahrenheit);

            if (Fahrenheit <= -50)
            {
                Console.ForegroundColor = ConsoleColor.White;
            }
            if (Fahrenheit <= -10)
            {
                Console.ForegroundColor = ConsoleColor.Blue;
            }
            if (Fahrenheit == 0)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
            }
            if (Fahrenheit >= 10)
            {
                Console.ForegroundColor = ConsoleColor.Red;
            }
            if (Fahrenheit >= 50)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.ReadLine();

            }

            // Convert Fahrenheit to Celsius.
            if (UserChoice == 2)
            {
                Console.SetCursorPosition(20, 0);
                Console.WriteLine("Converting Fahrenheit To Celsius");
                Console.SetCursorPosition(0, 4);
                Console.WriteLine("Enter the Temperature in Fahrenheit(°F) : ");
                Fahrenheit = int.Parse(Console.ReadLine());
                Celsius = (Fahrenheit - 32) * 5 / 9;
                Console.WriteLine("The temperature in Celsius is(°C) : " + Celsius);
                if (Celsius <= -50)
                {
                    Console.ForegroundColor = ConsoleColor.White;
                }
                if (Celsius <= -10)
                {
                    Console.ForegroundColor = ConsoleColor.Blue;
                }
                if (Celsius == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                }
                if (Celsius >= 10)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                }
                if (Celsius >= 50)
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.ReadLine();

                    //Exit
                    if (UserChoice != 3)
                    {
                        Console.ReadLine();
                        //Help Facility
                        if (UserChoice == 4)
                        {
                            Console.SetCursorPosition(20, 0);
                            Console.WriteLine("Welcome to the Temperature Calculator Help Facility");
                            Console.WriteLine("");
                            Console.ReadLine();
                        }

                        {


                        }
                    }
                }
            }
        }
    }
  }
}

Celsius到fahrenheit计算器控制台应用程序C#

您需要更改逻辑的顺序。在更改ForegroundColor之前,您正在将字符串写入Console。移动此if's(检查温度)

if (Fahrenheit <= -50)
{
     Console.ForegroundColor = ConsoleColor.White;
}

之前:

Console.WriteLine("The temperature in Fahrenheit is(°F) : " + Fahrenheit);

您的代码在写入控制台后正在更改颜色。之前更改它将影响打印文本的颜色。

同时使用if else而不是多个if's

所以它应该看起来像:

  1. 从输入中获取值
  2. 转换
  3. 根据转换值设置颜色
  4. 打印输出

您还应该将if提取到某个私有方法。您有重复的代码。