increment not working in an equation outside loop

Jonny123

Member
Joined
Oct 2, 2015
Messages
13
Programming Experience
Beginner
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 answer = 0;
            int total_right = 0;
            int total_wrong = 0;
            string name;
            double average;


            Console.WriteLine("Please enter a student name: ");
            name = Convert.ToString(Console.ReadLine());


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


                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);
                Console.ReadLine();
            }
      }             
}
 
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.
 
Back
Top Bottom