您的位置:首页 > 移动开发 > Objective-C

Objective-C入门02:一些简单的基本操作

2015-09-14 16:28 344 查看

Objective-C入门02:一些简单的基本操作

程序启动成功后,就会进入这个方法。

- (BOOL)application:(UIApplication
*)application didFinishLaunchingWithOptions:(
NSDictionary *)launchOptions {

c语言的字符串是""oc为了区分,前面加一个@

oc的打印,  自带换行符,   带有打印时间。

c语言常用的基础类型   int   long

oc语言中 使用 
NSInteger 
代替 int  long

  NSLog(@"asdflkasdfklsadf" );

  NSLog(@"a is %d,b is %d ,c is %f",12, 33, 28.11  )  ;

    

c语言的调用
  function ( ) ;

    

oc语言的调用
  [selffunction ] ;

    

判断奇数偶数调用
  [selffunctionJiOu : 123 ] ;

    

判断从AB之间所有数字的和调用
  [selfqiuHe : 100  : 1 ] ;

    

数字AB,能同时被数字cd整除的数字的数量
  [selfshu1 : 1 shu2
: 100 
shu3 : 10 shu4 : 5 ] ;

    

    returnYES ;
}

c语言的方法
void function ()
{

    printf("我是c语言函数\n"
) ;

}

oc语言的方法
-(void )function
{

    NSLog(@"我是oc语言方法"
) ;

}

判断从AB之间所有数字的和;

多参数之间,使用(空格+:):号隔开;

多参数  :前面可以加描述,让他人读代码跟容易。
-  (void ) qiuHe :  (NSInteger
)number1   :  (
NSInteger )number2
{
    NSInteger maxNum = [selfgetMaxNumWithNum
: number1
anotherNum : number2 ] ;
    NSInteger minNum  = [selfgetMaxNumWithNum
: number1
anotherNum : number2 ] ;
   NSInteger sum ;
   for (NSInteger  i  =  minNum 
;  i  <=  maxNum ;  i++ )

    {
        sum+= i ;
    }
   NSLog(@"sum is %ld" ,  sum )
;

}

数字AB,能同时被数字cd整除的数字的数量
- (void ) shu1 : (NSInteger )number1
               shu2 : (NSInteger )number2
               shu3 : (NSInteger )number3
               shu4 : (NSInteger )number4
{
   NSInteger sum  =  0  ;
   NSInteger maxNum = [selfgetMaxNumWithNum
: number1
anotherNum : number2 ] ;
   NSInteger minNum =  [selfgetMinNumWithNum
: number1
anoutherNum : number2 ] ;
   for (NSInteger  i =  minNum 
;  i  <=  maxNum  ; i++ )

    {
      if(( (i%number3)==0)&&((i%number4)==0))
        sum++;
    }
   NSLog(@"%ld%ld的数中能被%ld%ld整除的数量为%ld",number1,number2,number3,number4,sum);
}

方法:返回两个数字中,较大的数
-  (NSInteger )getMaxNumWithNum : (NSInteger
)num

                                   anotherNum : (NSInteger )anotherNum
{
   return  num > anotherNum ? num : anotherNum ;
}

方法:返回两个数字中,较小的数
- (NSInteger )getMinNumWithNum : (NSInteger
)num

                                anoutherNum : (NSInteger )anoutherNum
  {
     return  num > anoutherNum ?  anoutherNum : num ;
  }

把三个数按小到大输出
- (void )getMinGoMax : (NSInteger
) num1

             getMinGoMax : (NSInteger ) num2
             getMinGoMax : (NSInteger ) num3
{
num1 = [selfgetMinNumWithNum :
num1
anoutherNum : num2 ] ;
num2 = [self getMaxNumWithNum :
num1
anotherNum : num2 ] ;
}

判断奇数偶数
-(void )functionJiOu : (int
a3cc
) number

  {     

使用%@打印字符串

如果是奇数,打印出number是奇数,否则打印出number是偶数
     NSLog(@" %d 
%@ "
,  number ,  number%2 ==  0  ?  @"偶数"
:
@"奇数");
  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  objective-c xcode