OC -- 框架 練習(xí)

/*
實(shí)際一個(gè)學(xué)生類Student,有屬性:name,age,score,classNum
1.將如下學(xué)生添加到數(shù)組中
姓名 年齡 分?jǐn)?shù) 班級(jí)
Tom 17 82 Class01
Jim 22 75 Class01
Jerry 34 54 Class01
Owen 22 98 Class04
Steve 19 77 Class05

2.計(jì)算所有學(xué)生的平均分 (年級(jí)的平均分)
3.用名字作為key,value是學(xué)生對(duì)象,將這些學(xué)生存入字典
4.計(jì)算各個(gè)班級(jí)的平均分
*/

Student.h

//設(shè)置屬性
@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"];
    //......分別設(shè)置5個(gè)學(xué)生信息

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;//一班的人數(shù)

//        。。。。。。

//遍歷學(xué)生 數(shù)組
for (int i = 0;i < [array count];i++)
{
  Student *stu = [array objectAtIndex:i];

//計(jì)算2班的 人數(shù)和總分
if([stu.classNum isEqualToString:@"Class01"])
{
  class1count ++;
  class1sum += sut.score;
}

//計(jì)算4班的 人數(shù)和總分
    // ......

}

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容