zoukankan      html  css  js  c++  java
  • ObjectiveC语言学习(三)

    主要内容

    1. for循环
    2. while循环
    3. 控制台输入

    -------我是分割线--------

    1.for循环

    <For.h>

     1 #import <Foundation/Foundation.h>
     2 
     3 @interface For : NSObject
     4 {
     5     int sum;
     6 }
     7 
     8 - (id) init;
     9 
    10 - (int) forFrom:(int)fromNum andTo:(int)toNum;    //定义一个函数为forFrom:andTo:
    11 
    12 
    13 @end

    <For.m>

     1 #import "For.h"
     2 
     3 @implementation For
     4 
     5 - (id) init
     6 {
     7     sum = 0;
     8     return self;
     9 }
    10 
    11 - (int) forFrom:(int)fromNum andTo:(int)toNum
    12 {
    13     for (; fromNum <= toNum; fromNum++)  //for循环体
    14     {
    15         sum += fromNum;
    16     }
    17     return sum;
    18 }
    19 @end

    2.while循环

    <While.h>

     1 #import <Foundation/Foundation.h>
     2 
     3 @interface While : NSObject
     4 {
     5     int sum;
     6 }
     7 
     8 - (id) init;
     9 - (int) whileFrom:(int)fNum andTo:(int)tNum;  //定义函数为whileFrom:andTo:
    10 
    11 @end

    <While.m>

     1 #import "While.h"
     2 
     3 @implementation While
     4 
     5 - (id) init
     6 {
     7     sum = 0;
     8     return self;
     9 }
    10 
    11 - (int) whileFrom:(int)fNum andTo:(int)tNum
    12 {
    13     while (fNum <= tNum)
    14     {
    15         sum += fNum;
    16         fNum++;
    17     }
    18     return sum;
    19 }
    20 
    21 @end

    <main.m>

     1 #import <Foundation/Foundation.h>
     2 #import "For.h"
     3 #import "While.h"
     4 
     5 int main(int argc, const char * argv[])
     6 {
     7 
     8     @autoreleasepool
     9     {
    10         int fNum = 1;
    11         int tNum = 100;
    12         int sum = 0;
    13         // insert code here...
    14         NSLog(@"Hello, World!");
    15     
    16         For *f = [[For alloc]init];
    17         sum = [f forFrom:fNum andTo:tNum];
    18         NSLog(@"From %d To %d Sum is %d", fNum, tNum, sum);
    19         
    20         While *w = [[While alloc]init];
    21         sum = [w whileFrom:fNum andTo:tNum];
    22         NSLog(@"From %d To %d Sum is %d", fNum, tNum ,sum);
    23     }
    24     return 0;
    25 }

     3.控制台输入

     1 #import <Foundation/Foundation.h>
     2 #import "For.h"
     3 #import "While.h"
     4 
     5 int main(int argc, const char * argv[])
     6 {
     7 
     8     @autoreleasepool
     9     {
    10         int fNum = 0;
    11         int tNum = 0;
    12         int sum = 0;
    13         // insert code here...
    14         NSLog(@"Hello, World!");
    15         
    16         printf("Please input fromNum: ");
    17         scanf("%d", &fNum);   //从屏幕输入fNum
    18         printf("Please input toNum: ");
    19         scanf("%d", &tNum); //从屏幕输入tNum
    20         
    21         For *f = [[For alloc]init];
    22         sum = [f forFrom:fNum andTo:tNum];
    23         NSLog(@"For From %d To %d Sum is %d", fNum, tNum, sum);
    24         
    25         While *w = [[While alloc]init];
    26         sum = [w whileFrom:fNum andTo:tNum];
    27         NSLog(@"While From %d To %d Sum is %d", fNum, tNum ,sum);
    28     }
    29     return 0;
    30 }
  • 相关阅读:
    [iOS 多线程 & 网络
    [iOS 多线程 & 网络
    [iOS 多线程 & 网络
    [iOS 多线程 & 网络
    [iOS 多线程 & 网络
    [iOS UI进阶
    Python基础
    24种编程语言的Hello World程序
    python中的输入和输出
    第一个Python程序
  • 原文地址:https://www.cnblogs.com/ADaii/p/2849152.html
Copyright © 2011-2022 走看看