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);
}
}
}
Я прокомментировал, что происходит в моем коде, насколько это возможно. Программа хранит Fahrenheit как строку и преобразует ее в десятичный штраф. Однако celcius = 0 вместо правильного числа в точке, где celcius = celcius = ((fahrint) - 32) * (5/9) ;. Я знаю, что я неправильно указал celcius, но я не думаю, что это повлияло на код. Любые решения?Почему моя программа C# возвращает 0?
Спасибо!
Это странно объявить '5' как' double' а затем произведите результат как десятичное. Если вы объявляете 5 как «десятичную», вам не потребуется бросок. –
Хороший звонок. Я не проверял окончательный тип. Используйте десятичную, а не двойную. – Jonesopolis
Не используйте десятичные числа для вычислений. Используйте двойной. (Десятичный лучше всего подходит для финансовых расчетов.) –