欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2.2 9x9乘法表

程序员文章站 2022-07-07 17:38:39
...
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createTable];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//创建九九乘法表
- (void)createTable{
    //在外部去写一个宽高
    float width = 90;
    float height = 40;
    
    
    
    
    for (int i = 1; i < 10; i ++)
    {
        for (int j = 1; j <= i; j++) {
            //控制输出
            //printf("%d * %d = %d ",i, j, i*j);
            //再这里,由控制台输出改为用Label去做
            UILabel *label = [[UILabel alloc] init];
            
            //每个label 显示内容都是一个式子
            label.text = [NSString stringWithFormat:@" %d * %d = %d",i,j,i*j];
            
            label.frame = CGRectMake((j - 1 )* width ,(i - 1 +1) * height, width, height);
            
            if ((i +j) %2 !=0){
                label.backgroundColor = [UIColor redColor];
            }
            else {
                label.backgroundColor = [UIColor yellowColor];
            }
            //添加到屏幕上显示
            [self.view addSubview:label];
            
        }
    }
    printf("\n");
}

@end