如何在学校失败率中获得 c# 的百分比

How to get percentage on c# in school fail rate

Hi there. I just wanted to get the fail rate in score of the exam and I tell you here what each variable name means:

*

tedad= number of student//- tedad fale = number of students who fails//- darsad gaboli= percentage of students who pass the exam//- tedad nomarat ra vared konid = enter the amount of numbers you wanna enter//- Tedad ra dorost vared konid= enter the numbers in the correct format (be aware in my country scores are from 20 means 0-20 and if you get under 10 you will fale)// ignore comments in code :)//- Majmo nomre: sum of scores//-

* the main problem is float a contains 0 when I divide tedadfail with tedad but when I do the opposite it shows the correct number how can I fix this?

int tedad, tedadfaile = 0;
    float avg, sum = 0;
    //gereftan tedad
    Console.WriteLine("Tedad ra vared konid: ");
    while (1 > 0)
    {
        tedad = Convert.ToInt32(Console.ReadLine());
        if (tedad<=0)
        {
            Console.WriteLine("Tedad ra dorost vared konid.");
            continue;
        }
        if(tedad>0)
        {
            break;
        }
    }

    float[] scorre = new float[tedad];
    
    //gereftan nomre

    Console.WriteLine("Nomarat ra vared konid: ");
    
    for(int i=0; i<tedad;i++)
    {
        scorre[i] = Convert.ToInt32(Console.ReadLine());

        if (scorre[i] < 0 || scorre[i] > 20)
        {
            Console.WriteLine("Lotfan nomre dorost ra vared konid.");
            continue;
        }

        if(scorre[i] < 10)
        {

            tedadfaile=tedadfaile+1;
            
        }
    }
    
    //jam kardan nomre

    for(int i=0; i<tedad;i++)
    {
        sum = sum + scorre[i];
    }

    //miangin

    avg = sum / tedad;

    //darsad gaboli

    float a =(tedadfaile)/( tedad);
    float b = 100 - a;
    
    Console.WriteLine(" Nomarat dar class " + tedad +" nafari:\nMajmo nomre:"+sum+"\nMIangin:"+avg+"\nMiangin gaboli: "+b+" darsad");

你不是第一个犯这个错误的人:

float a =(tedadfaile)/( tedad);

tedadfailetedad都是整数。
因此,您所做的是将两个整数相除,这自动意味着您正在使用整数除法,然后才将结果转换为浮点值。

您可以执行以下操作以强制执行浮点运算:

float a =((float)tedadfaile)/( tedad);

意思是:至少将其中一个数转为浮点数,以强制浮点运算。