简体   繁体   中英

Any suggestion how can I do this better?

First I would like to say sorry that I didn't use english but you will have the general idea of what I am trying to do.

Create class subjects variables and characteristics / properties: - kodiILendes - emriILendes - nota The method Main should make it possible for users to introduce these data, code file, the file name and grade. You need to create objects for 5 subjects Make use of accessories (get and set), to mark So while you should assign grades should not be less than 5 and also must not be greater than 10. And to submit your average for this semester format, p.sh .: "Your Average It is 9.3 ".

class Lendet
{
    public int kodiIlendes;
    public string emriIlendes;

    private int nota;
    public int Nota {
        get {
            return nota;
        }
        set {
            if (value > 5 && value <= 10)
            {
                nota = value;
            }
            else {
                Console.WriteLine("Nota duhet te jet me e > se 5 dhe nuk duhet te jet me e > se 10 ");
            }
        }
    }

}


 static void Main(string[] args)
        {
            Lendet Anglisht = new Lendet();
            Anglisht.kodiIlendes = 100;
            Anglisht.emriIlendes = "Anglisht";
            Anglisht.Nota = 10;
        }

Now lets imagine I created the 5 objects and I want to find the average.How can I do that ? One way is like this Console.WriteLine(x.Nota+y.Nota+z.Nota+b.Nota+c.Nota/5)

class Lendet
{
    public int kodiIlendes;
    public string emriIlendes;
    public static float sum;    
    public static int count;

    public Lendet()
{
count++;
}
    private int nota;
    public int Nota {
        get {
            return nota;
        }
        set {
            if (value > 5 && value <= 10)
            {
                sum =sum+value;
                nota = value;
            }
            else {
                Console.WriteLine("Nota duhet te jet me e > se 5 dhe nuk duhet te jet me e > se 10 ");
            }
        }
    }

}

static void Main(string[] args)
        {
            //create object1
            // create object2
           //......create object n
          Console.WriteLine(Lendet.sum/Lendet.count);
        }

create two static variables, one for count of objects created and other for sum. Divide second by first to get the average.

My approach, with history:

class Lendet
{
     public int Nota { get; private set; }
     public Lendet(int nota)
     {
          this.Nota = nota;
          LendetHistory.Add(this);
     }
}

static class LendetHistory
{
     private static List<Lendet> lendets = new List<Lendet>();
     public static float Average()
     {
          if(lendets.Count < 1)
                  return 0;

          return lendets.Select(s => s.Nota).Average();
     }
     public static void Add(Lendet lendet)
     {
          lendets.Add(lendet);
     }
}

use in code:

  var k = new Lendet(10);
  var c = new Lendet(20);
  Console.WriteLine(LendetHistory.Average());

and with that approach you can expand your logic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM