There are several things that need correcting in your code.
You are using
double
s where
int
s will do.
You are testing if x ==
AntalKast
. Unless
AntalKast
is 0, this will never be true so your loop will never execute.
You are generating the new Random inside the loop. This will normally result in the same numbers being generated - move it outside of the loop.
All paths need to return a value, if the loop doesn't execute then there is no return. Move the definition of the return value before the loop and the actual return after.
This seems very strange - are you sure this is what you meant?
Resultatet = TarningsResultat;
Resultatet = TarningsResultat + Resultatet;
It's normal (but not required) to have method names start with an uppercase character but parameters and fields start with a lowercase character.
The fix to these results in this:
using System;
namespace ConsoleApplication1
{
class Program
{
static int KastaTärning(int antalKast)
{
Console.Clear();
int resultatet = 0;
Random randomerare = new Random();
for (int x = 0; x < antalKast; x++)
{
int tarningsResultat = randomerare.Next(1, 6);
Console.Write("Dice: {0} ", tarningsResultat);
resultatet += tarningsResultat;
}
Console.WriteLine();
return resultatet;
}
static void Main(string[] args)
{
Console.WriteLine("Hur många kast? ");
int antalKast = int.Parse(Console.ReadLine());
Console.WriteLine("Ditt resultat är: " + KastaTärning(antalKast));
Console.ReadKey();
}
}
}