OC -- 框架 练习

/*
实际一个学生类Student,有属性:name,age,score,classNum
1.将如下学生添加到数组中
姓名 年龄 分数 班级
Tom 17 82 Class01
Jim 22 75 Class01
Jerry 34 54 Class01
Owen 22 98 Class04
Steve 19 77 Class05

2.计算所有学生的平均分 (年级的平均分)
3.用名字作为key,value是学生对象,将这些学生存入字典
4.计算各个班级的平均分
*/

Student.h

//设置属性
@property(nonatomic,copy)NSString *name;
@property(nonatomic,assign)NSInteger age;
@property(nonatomic,assign)float score;
@property(nonatomic,copy)NSString *classNum;

- (instancetype)initWithName:(NSString *)name withAge:(NSInteger)age withScore:(float)score withClassNum:(NSString *)classNum;

Student.m

- (instancetype)initWithName:(NSString *)name withAge:(NSInteger)age withScore:(float)score withClassNum:(NSString *)classNum
{
  if (self = [super init])
    {
      _name = name;
      _age = age;
      _score = score;
      -classNum = classNum;
    }
    return self;
}

main.m

//1.
Student *tom = [[Student alloc]initWithName:@"Tom" withAge:17 withScore:82 withClassNum:@"Class01"];
    //......分别设置5个学生信息

NSArray *array = @[tom,jim,jerry,owen,seve];

//2.
float sum = 0;

for (Student *stu in array)
{
  sum += stu.score;
}

NSLog(@"总分为 %f",sum);

NSLog(@"平均分为%f",sum/[array count]);

//3.
NSDictionary *dic = @{
                                    @"tom":tom,
                                      //分别放入......
                                  }


//4.
float class1sum = 0;//一班的总分
NSInteger class1count = 0;//一班的人数

//        。。。。。。

//遍历学生 数组
for (int i = 0;i < [array count];i++)
{
  Student *stu = [array objectAtIndex:i];

//计算2班的 人数和总分
if([stu.classNum isEqualToString:@"Class01"])
{
  class1count ++;
  class1sum += sut.score;
}

//计算4班的 人数和总分
    // ......

}

NSLog(@"一班平均分为%f",class1sum/class1count);

你可能感兴趣的:(OC -- 框架 练习)