简体   繁体   English

摄氏到华氏度的计算器控制台应用程序C#

[英]Celsius to fahrenheit calculator console application C#

I have written this code for my Celsius to Fahrenheit and vice versa converter. 我已经为我的摄氏到华氏度编写了此代码,反之亦然。 everything seems to be working fine. 一切似乎都正常。 the only thing that is not going well is that when the user gets an converted temperature the text colour should change depending on the temperature. 唯一不好的是,当用户获得转换的温度时,文本颜色应根据温度而变化。 I will also post my code underneath and can anyone tell me if i have done anything wrong and what I have to do to fix it Thank you I really appreciate it 我还将在下面发布我的代码,任何人都可以告诉我是否做错了什么以及如何解决它,谢谢。我非常感谢

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();
                        }


                        {




                        }
                    }
                }
            }
        }
    }
  }
}

You need to change order of your logic. 您需要更改逻辑顺序。 You are writing string to Console before you are changing ForegroundColor. 在更改ForegroundColor之前,您正在向控制台写入字符串。 Move this if's (checking for temperature) 如果移动(检查温度)

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

}

before: 之前:

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

Your code is changing color after writing to console. 写入控制台后,您的代码正在更改颜色。 Changing it before will affect color of printed text. 之前更改它会影响打印文本的颜色。

Also use if else instead of multiple if's . 还可以使用if else而不是多个if's

So it should look like: 所以它应该看起来像:

  1. Get value from input 从输入中获取价值
  2. Convert 兑换
  3. Set color depending on converted value 根据转换值设置颜色
  4. Print output 打印输出

You should also extract your if's to some private method. 您还应该将if提取为某种私有方法。 You have duplicated code. 您有重复的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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