我已将此代码编写为我的Celsius to Fahrenheit,反之亦然。 一切似乎都很好。唯一不顺利的是,当用户获得转换温度时,文本颜色应根据温度而改变。 我也会在下面发布我的代码,任何人都可以告诉我,如果我做错了什么,我需要做些什么来解决它 谢谢,我真的很感激
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();
}
{
}
}
}
}
}
}
}
}
答案 0 :(得分:1)
您需要更改逻辑的顺序。在更改ForegroundColor之前,您正在将字符串写入Console。移动这个if(检查温度)
if (Fahrenheit <= -50)
{
Console.ForegroundColor = ConsoleColor.White;
}
之前:
Console.WriteLine("The temperature in Fahrenheit is(°F) : " + Fahrenheit);
写入控制台后,您的代码正在改变颜色。之前更改它将影响打印文本的颜色。
同时使用if else
代替多个if's
。
所以看起来应该是这样的:
你还应该将你的if提取到一些私有方法。你有重复的代码。