显示循环计数

时间:2012-04-18 19:18:20

标签: objective-c for-loop cycle

我有三个变量,一个叫A = 2012,第二个叫做B = 2020,第三个是C = B - A.C是年份的差异。现在我想在屏幕上看到两个日期之间的年份: 2012 2013 2014 2015年 2016 2017年 2018 2019 2020年 我尝试了不同的解决方案,但我不能这样做,我认为正确的方法是一个循环。这样对吗?你能告诉我怎么做吗?谢谢。

2 个答案:

答案 0 :(得分:1)

我认为你的意思是循环循环? 你可以试试这个:

int a = 2012;             // Holds the first year
int b = 2020;             // Holds the last year
int c = b - a;            // Holds the difference of the a and b
while(a<=b) {             // Execute this as long as a is equal or less then b
    NSLog("Year: %d", a); // Print the year to the console
    a++;                  // Increment the value of a with 1
}

答案 1 :(得分:-2)

for(int i=0;i<=c;i++)
{
 NSLog(@"The Year is %i \n",a+i);
}