Förstår inte felkoden jag får?? C#
Hej!
Trodde att jag var klar med uppgiften men får nu en felkod jag inte alls förstår inte kan inte lösa den.
Detta är en skoluppgift (amerikanen i basun) så har vissa rama att hålla mig innanför, typ som att man ska kommentera koden kanske lite onödigt mycket.
Och jag är fullt medveten om att det är massa felstav i kommentarerna, har dyslexi och ska självklart rätta felstavningarna innan jag skickar in den.
Vill bara få en förklaring på varför den helt plötsligt ger en fel kod när jag använder ''cel'' trotts att det gått hur bra som helst i resten av koden.
Bifogar koden här
using System;
namespace Bastu
{
class Program
{
public static double FahrToCel(int fahr)//method that takes in int fahr (fahrenheit) but converts it to double cel (celcius) and returns cel
{
double cel = Convert.ToDouble(fahr); //converts int to double
cel = (fahr - 32.0) * 5.0 / 9.0; //converts fahernheit to celsius
return cel;
}
static void Main(string[] args)
{
double maxTemp = 77.0; // deklare values so we leter on can use the verible .
double minTemp = 73.0;
double optiTemp = 75.0;
double cel;
int fahr;
Console.WriteLine("Welcome to the Sauna!");
do // the loop starts with asking the user to enter a temerature
{
Console.Write("Please enter a temperature in fahrenheit: ");
try // The ''try'' will catch a mistake for exemeple is the user write a letter insded of a number
{
fahr = int.Parse(Console.ReadLine()); // the number is read and given the verible fahr
cel = FahrToCel(fahr); // the metod FahrTocel is calld to make fahr into cel, witch the program will use from now on
}
catch (Exception)// If the use did wirte a letter insede of a number this will happend
{
Console.WriteLine("Please enter only numbers");
continue;// So the loop starts from the begining agin and not just stops here
}
if (cel > maxTemp)//if the temp is to high the program will as for a new number, as well as telling the use what the temp in now(with only one desimal)
{
Console.WriteLine("The Sauna is too hot! Its " + Math.Round(cel, 1) + " celcius. Turn down the temperature!");
}
else if (cel < minTemp)//if the temp is to low the program will as for a new number, as well as telling the use what the temp in now(with only one desimal)
{
Console.WriteLine("The Sauna is too cold! Its only " + Math.Round(cel, 1) + " celcius. Turn up the temperature!");
}
else if (cel == optiTemp)//if this is true the loop will end here beacuse the temp is perfect. And the code in this block will aper.
{
Console.WriteLine("The Sauna now has a perfect temperature of " + Math.Round(cel, 1) + "celcius! Have a nice visit!");
}
else//if non of the above was true this will show beacuse the temp is in den middel of the lowset and highes accepteble, and the loop will end here.
{
Console.WriteLine("The sauna has a decent temperature now of " + Math.Round(cel, 1) + " celcius! Enjoy");
}
}
while (cel >= minTemp || cel <= maxTemp); //deturns how long the loop will continue.
Console.ReadKey();
}
}
}
while (cel >= minTemp || cel <= maxTemp); //deturns how long the loop will continue.
Här visar den felet: Error CS0165: Use of unassigned local variable 'cel' (CS0165) (bastun3)
varför visar den det bara här??