using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fahrenheit_to_Celsius_Converter
{
    class Program
    {
        static void Main(string[] args)
        {//Prompt user to enter a temperature
            Console.WriteLine("Please enter the temperature in fahrenheit you want to convert");
            //Sets variable fahr to the number entered
            string fahr = Console.ReadLine();
            //Just for testing debugging purposes displays what data is stored in fahr.
            Console.WriteLine(fahr);
            //Initializes an integer fahrint
            int fahrint;
            fahrint = int.Parse(fahr);
            //Just for testing debugging purposes displays what data is stored in fahrint.
            Console.WriteLine(fahrint);
            //Initializes an integer celcius
            decimal celcius;
            //Does the calculation that stores to a variable called celcius
            celcius = ((fahrint) - 32) * (5 / 9);
            //At this point the celcius variable print 0. It should print 5 if 41 is entered
            Console.WriteLine(celcius);
            string celciusstring;
            celciusstring = celcius.ToString();
            Console.WriteLine(celciusstring);



        }
    }
}


我已经尽可能多地评论了代码中正在发生的事情。该程序将华氏温度存储为字符串,并将其转换为十进制罚款。但是,在celcius = celcius =((fahrint)-32)*(5/9);的点上,celcius = 0而不是正确的数字。我知道我拼写的celcius错误,但我不认为它影响了代码。有什么办法吗?

谢谢!

最佳答案

整数部分。 5 / 9始终为0

(fahrint - 32) * (5 / 9)
                   ^^^


您需要将这些值中的至少一个强制转换为decimal

celcius = (fahrint - 32) * (5M / 9);
//5 is of type Decimal now

07-26 09:34