[英]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
。
所以它应该看起来像:
您还应该将if提取为某种私有方法。 您有重复的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.