簡體   English   中英

攝氏到華氏度的計算器控制台應用程序C#

[英]Celsius to fahrenheit calculator console application C#

我已經為我的攝氏到華氏度編寫了此代碼,反之亦然。 一切似乎都正常。 唯一不好的是,當用戶獲得轉換的溫度時,文本顏色應根據溫度而變化。 我還將在下面發布我的代碼,任何人都可以告訴我是否做錯了什么以及如何解決它,謝謝。我非常感謝

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


                        {




                        }
                    }
                }
            }
        }
    }
  }
}

您需要更改邏輯順序。 在更改ForegroundColor之前,您正在向控制台寫入字符串。 如果移動(檢查溫度)

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提取為某種私有方法。 您有重復的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM