本文介绍了转换到华氏摄氏度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想华氏温度转换成摄氏结果
做以下我总是得到零:
I'm trying to convert a Fahrenheit temperature into Celsius.
doing the following I'm always getting zero:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Celcius_Farenheit_Converter
{
class Program
{
static double Celcius(double f)
{
double c = 5/9*(f - 32);
return c;
}
static void Main(string[] args)
{
string text = "enter a farenheit tempature";
double c = Celcius(GetTempature(text));
Console.WriteLine("the tempature in Celicus is {0}", c);
Console.ReadKey(true);
}
static double GetTempature(string text)
{
Console.WriteLine(text);
bool IsItTemp = false;
double x = 0;
do
{
IsItTemp = double.TryParse(Console.ReadLine(), out x);
} while (!IsItTemp);
return x;
}
}
}
可以吗帮我解决这个问题?
can you help me fix it?
推荐答案
5/9
执行整数除法&MDASH ;也就是说,它总是丢弃小数部分—所以它总是返回0。
5/9
performs an integer division—that is, it always discards the fractional part—so it will always return 0.
5.0 / 9.0
执行浮点除法,并返回预期0.55555 ...
5.0/9.0
performs floating-point division, and will return the expected 0.55555...
试试这个:
static double Celcius(double f)
{
double c = 5.0/9.0 * (f - 32);
return c;
}
进一步阅读
- / Operator (C# Reference)
这篇关于转换到华氏摄氏度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!