我目前在C#控制台中使用Visual Studio,我的主要问题是没有出现下面这行。我没有继续讨论if语句,因为当我运行它时,console将运行第一个Console.WriteLine,我将输入第一个输入,然后console将在下一次按键时关闭,而不运行第二个Console.WriteLine。这就是我的问题。
Console.WriteLine("Please enter F or C to define your temperature. Enter F or C here: ");我测试了第一个输入,但它拒绝在控制台中读取该行,并且似乎在第二个Console.WriteLine之后停止。另外,如果任何人有任何关于如何进行温度转换的提示,无论是F还是C都可以输入,这将与如何解决我的问题一样值得感谢。
{
class Program
{
static void Main(string[] args)
{
int numberOne;
int unConvertedTemp;
bool fahrenheit;
bool celsius;
Console.Write("Hello welcome to the temperature converter. Please provide the numerical value of your temperature. Enter your response here:" );
numberOne = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter F or C to define your temperature. Enter F or C here: ");
unConvertedTemp = Convert.ToInt32(Console.ReadLine());
int fToC = ((F - 32) / 9) * 5;
int cTOF = ((C + 32 / 5) * 9);
}
}
}发布于 2017-01-15 12:59:19
我不相信你说的是真的。你说它会在下一次按键时关闭,但它只会在下一次按下"enter“按钮时退出。
在看到任何输出之前,您的函数(和程序)只是退出。只需在最后的WriteLine后面添加一个ReadLine,这样控制台就会等待您键入"enter“,然后再退出。
发布于 2017-01-15 12:26:26
不太确定你的问题是什么,但这应该会有帮助。您需要使用if语句来检查用户输入是什么。
static void Main(string[] args)
{
int tempurature;
string celciusOrFahrenheit;
Console.Write("Hello welcome to the temperature converter. Please provide the numerical value of your temperature. Enter your response here:");
tempurature = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter F or C to define your temperature. Enter F or C here: ");
celciusOrFahrenheit = Console.ReadLine();
if (celciusOrFahrenheit.ToUpper() == "C")
{
Console.WriteLine("Temp converted to farenhite: " + ConvertToFahrenheit(tempurature));
}
if (celciusOrFahrenheit.ToUpper() == "F")
{
Console.WriteLine("Temp converted to celcius: " + ConvertToCelcius(tempurature));
}
Console.ReadKey();
}
public static int ConvertToFahrenheit(int temp)
{
int cToF = ((temp + 32 / 5) * 9);
return cToF;
}
public static int ConvertToCelcius(int temp)
{
int fToC = ((temp - 32) / 9) * 5;
return fToC;
}
}https://stackoverflow.com/questions/41657684
复制相似问题