Har en uppgift med följande kod:
Problemet är att functionen scania.Calc_Topspeed(horsepower, last) (halvnära slutet) inte vill köras, på grund av felmeddelandet på villkoren att "Cannot convert int to horsepower/last".
Och jag antog att det skulle gå plocka variablerna från metoden Scania() i klassen Run(), och använda som villkor i den metoden.
Men det verkar inte gå, fastän samma funktion går bra i metoden Volvo(), fast för klassen Fordon istället för den Lastbil : Fordon.
Några idéer?
Kod:
using System;
namespace Fordon_Lastbil
{
internal class Program
{
static void Main(string[] args)
{
Fordon volvo = new Fordon(90, "grön");
Lastbil scania = new Lastbil(400, "vit", 3000);
Run();
}
static void Run()
{
int temp = 0;
do
{
Console.Clear();
Console.WriteLine("Välkommen till fordonssimulatorn.");
Console.WriteLine("Välj fordon:");
Console.WriteLine("1 Volvo - bil");
Console.WriteLine("2 Scania - lastbil");
Console.Write("\nDitt val: ");
try
{
temp = int.Parse(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("Var god välj en siffra bland menyalternativen.\n" +
"Tryck vilken knappt som helst för att återgå till menyn.");
Console.ReadKey(true);
Run();
}
switch (temp)
{
case 1:
Volvo();
break;
case 2:
Scania();
break;
case 0:
Console.WriteLine("Programmet avslutas.");
break;
default:
Console.WriteLine("Felaktig inmatning.");
Console.ReadKey();
break;
}
} while (temp != 0);
}
static void Volvo()
{
int horsepower=0;
string color = "vit";
Console.WriteLine("Hur många hästkrafter har bilen?");
horsepower = int.Parse(Console.ReadLine());
Console.WriteLine("Vilken färg är bilen?");
color = Console.ReadLine();
Fordon volvo = new Fordon(horsepower, color);
int temp = 0;
do
{
Console.Clear();
Console.WriteLine("Vad ska bilen göra?.");
Console.WriteLine("Välj alternativ:");
Console.WriteLine("1 Köra");
Console.WriteLine("2 Tuta");
Console.WriteLine("3 Huvudmeny");
Console.Write("\nDitt val: ");
try
{
temp = int.Parse(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("Var god välj en siffra bland menyalternativen.\n" +
"Tryck vilken knappt som helst för att återgå till menyn.");
Console.ReadKey(true);
Volvo();
}
switch (temp)
{
case 1:
Console.WriteLine("Den {0}a Volvon kör i {1} km/h", color, volvo.Calc_Topspeed(horsepower)); ;
Console.ReadKey();
break;
case 2:
volvo.Horn();
Console.ReadKey();
break;
case 3:
Run();
break;
case 0:
Console.WriteLine("Programmet avslutas.");
break;
default:
Console.WriteLine("Felaktig inmatning.");
Console.ReadKey();
break;
}
} while (temp != 0);
}
static void Scania()
{
int horsepower = 0;
string color = "vit";
int last = 0;
Console.WriteLine("Hur många hästkrafter har fordonet?");
horsepower = int.Parse(Console.ReadLine());
Console.WriteLine("Vilken färg är fordonet?");
color = Console.ReadLine();
Console.WriteLine("Hur många kilo är lastat på fordonet?");
last = int.Parse(Console.ReadLine());
Lastbil scania = new Lastbil(horsepower, color, last);
int temp = 0;
do
{
Console.Clear();
Console.WriteLine("Vad ska lastbilen göra?.");
Console.WriteLine("Välj alternativ:");
Console.WriteLine("1 Köra");
Console.WriteLine("2 Tuta");
Console.WriteLine("3 Huvudmeny");
Console.Write("\nDitt val: ");
try
{
temp = int.Parse(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("Var god välj en siffra bland menyalternativen.\n" +
"Tryck vilken knappt som helst för att återgå till menyn.");
Console.ReadKey(true);
Scania();
}
switch (temp)
{
case 1:
Console.WriteLine("Den {0}a Scanian kör i {1} km/h", color, scania.Calc_Topspeed(horsepower, last)); ;
Console.ReadKey();
break;
case 2:
scania.Horn();
Console.ReadKey();
break;
case 3:
Run();
break;
case 0:
Console.WriteLine("Programmet avslutas.");
break;
default:
Console.WriteLine("Felaktig inmatning.");
Console.ReadKey();
break;
}
} while (temp != 0);
}
}
class Fordon
{
protected int horsepower;
protected string color;
public Fordon (int horsepower, string color)
{
this.horsepower = horsepower;
this.color = color;
}
public virtual double Calc_Topspeed(int horsepower)
{
return horsepower * 1.5;
}
public void Horn()
{
Console.WriteLine("Du tutar.");
}
}
class Lastbil : Fordon
{
private int last; //kilogram last
public Lastbil(int horsepower, string color, int last) : base(horsepower, color)
{
this.last = last;
this.horsepower = horsepower;
this.color = color;
}
public override double Calc_Topspeed(horsepower, last)
{
return (horsepower * 1.5)/(last/7000);
}
}
}
Problemet är att functionen scania.Calc_Topspeed(horsepower, last) (halvnära slutet) inte vill köras, på grund av felmeddelandet på villkoren att "Cannot convert int to horsepower/last".
Och jag antog att det skulle gå plocka variablerna från metoden Scania() i klassen Run(), och använda som villkor i den metoden.
Men det verkar inte gå, fastän samma funktion går bra i metoden Volvo(), fast för klassen Fordon istället för den Lastbil : Fordon.
Några idéer?