Hej
jag sitter med samma uppgift och förstår inte hur jag ska koda try catch delen. Egentligen förstår jag inte mycket alls av den här kursen och tycker det är skit vilken "lärarhjälp" man får iom att man inte får någon. Svaret man får när man behöver hjälp är "googla. hitta svar online".
Känns sådär när man är helt ny på programmering och vill börja förbereda inför plugg längre fram.
Så kontentan är, om du ska döma ut en och dumförklara en så behöver du inte skriva något alls. Vill du däremot hjälpa och förklara på en pedagogisk nivå är du välkommen att svara så mycket du vill.
Jag har fått till all kod men får inte in try catch delen så koden fungerar. Så här har jag kodat.
/*
* Created by SharpDevelop.
* User: Ida
* Date: 2019-02-19
* Time: 15:00
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace Uppgift2_Amerikanaren
{
class Program
{
public static int FarenheitToCelsius(int farenheit)
{
int celsius = ((farenheit - 32) * 5/9); //metod för att beräkna farenheit till celsius
return celsius;
}
static void Main(string[] args)
{
int celsius;
do{
Console.WriteLine("Insert desired temperature for the sauna: ");
int farenheit = Convert.ToInt32(Console.ReadLine());
celsius = FarenheitToCelsius(farenheit);
Console.WriteLine("The temperature is " + celsius + " degrees celsius.");
if (celsius < 73){
Console.WriteLine("It's too cold for a sauna");
Console.ReadLine();
}
else if (celsius > 77)
{
Console.WriteLine("It's too hot for a sauna!");
Console.ReadLine();
}
else if (celsius == 75)
{
Console.WriteLine("It's the ultimate temperature for a sauna!");
Console.ReadLine();
}
else
{
Console.WriteLine("It's a good temp for a sauna!");
Console.ReadLine();
}
}
while(celsius <73 || 77< celsius);
}
}
}
Och denna kod fungerar jättebra så jag behöver inte bli "mer förvirrad" men att skriva om och ändra saker.
Min fråga är vart try och catch ska skrivas. Har tänkt att try ska skrivas in efter:
{
int celsius;
do{
Try
{
Kod jag inte vet
}
Console.WriteLine("Insert desired temperature for the sauna: ");
int farenheit = Convert.ToInt32(Console.ReadLine());
celsius = FarenheitToCelsius(farenheit);
Console.WriteLine("The temperature is " + celsius + " degrees celsius.");
catch
{
console.writeline("Please enter a number");
}
if (celsius < 73){
Console.WriteLine("It's too cold for a sauna");
Console.ReadLine();
}
etc
eller tänker jag fel??