自从我在输出中添加文本以来,它一直在向中添加数字inputTemperature。例如,如果用户输入“ 10 ”,则输出行将显示:“ -112摄氏度转换为...”,并且转换将是正确的。您对导致这种情况的发生有任何想法吗?
// VARIABLES
double inputTemperature; // Stores user input
double celsius; // Celsius value
double fahrenheit; // Farhenheit value
// INPUT
if (double.TryParse(txtTemperature.Text, out inputTemperature) == false) // Checking that user input is valid
{
MessageBox.Show("ERROR: Please enter a numeric temperature to convert."); // Error message that is displayed
txtTemperature.ResetText(); // Resets the form
txtTemperature.Focus(); // Places the user's cursor back in the Temperature textbox
}
// PROCESSING
if (optCelsius.Checked) // If the celsius button is clicked, then the temperature needs to be converted from fahrenheit
{
fahrenheit = double.Parse(txtTemperature.Text);
fahrenheit = ((fahrenheit - 32) * 5 / 9);
lblNewTemperature.Text = fahrenheit.ToString(inputTemperature + " degrees Celsius converts to " + fahrenheit + " degrees Fahrenheit.");
}
else if (optFahrenheit.Checked) // If the fahrenheit button is clicked, then the temperature needs to be converted from celsius
{
celsius = double.Parse(txtTemperature.Text);
celsius = ((celsius * 9) / 5 + 32);
lblNewTemperature.Text = celsius.ToString(inputTemperature + " degrees Fahrenheit converts to " + celsius + " degrees Celsius.");
}
相关分类