increment not working in an equation outside loop

Jonny123

Member
I am trying to get the program to display an average right of the ten questions i am asking, i dosn't seem to work properly.
C#:
Random randomNumbers = new Random();
int number;
int number_two;
int i = 0;
int total_right = 0;
int total_wrong = 0;
string name;
double average;

Console.WriteLine("Please enter a student name: ");

for (i = 0; i < 10; i++)
{
number = randomNumbers.Next(500);
number_two = randomNumbers.Next(500);
Console.WriteLine("{0} {1} {2}", number, " + ", number_two);

if (answer == number + number_two)
{
Console.WriteLine("Correct");
total_right++;
}
else if (answer != number + number_two)
{
Console.WriteLine("Wrong");
total_wrong++;
}
}
average = total_right / 10;[B]//<-----------------------this returns 0 for some reason[/B]
Console.WriteLine("{0}", "\n");
Console.WriteLine("{0} {1}", "Information for student: ", name );
Console.WriteLine("{0} {1}", total_right, "total answers right" );
Console.WriteLine("{0} {1}", total_wrong, "total answers wrong");
Console.WriteLine("{0} {1}", "The student average is: ", average);
}
}
}

jmcilhinney

C# Forum Moderator
Staff member
In C#, the division operator (/) is type-specific. If you divide an int by another int then the result is an int. If you want a double then you should divide a double by a double. Convert your total_right value to a double and use a double literal to divide it by.

Replies
2
Views
101
Replies
8
Views
267
Replies
1
Views
198
Replies
6
Views
355
Replies
3
Views
228