viewDidLoad 变成无限循环。帮助
这可能是最简单/最蹩脚的问题。
因此,我尝试在 viewDidLoad 方法中初始化一个值为 0 到 3 增量为 0.25 的数组,并且我可以在此处看到无限循环。
NSArray *pickArray3 = [[NSMutableArray alloc] init];
int i = 0;
//for(i = 0.25; i<=3; i=i+0.25)
while (i<3)
{
//NSString *myString = [NSString stringWithFormat:@"%d", i];
i=i+0.25;
NSLog(@"The Value of i is %d", i );
//[pickArray3 addObject:myString]; // Add the string to the tableViewArray.
}
NSLog(@"I am out of the loop now");
self.doseAmount=pickArray3;
[pickArray3 release];
这就是输出。
2011-06-01 11:49:30.089 Tab[9837:207] The Value of i is 0
2011-06-01 11:49:30.090 Tab[9837:207] The Value of i is 0
2011-06-01 11:49:30.091 Tab[9837:207] The Value of i is 0
2011-06-01 11:49:30.092 Tab[9837:207] The Value of i is 0
// And this goes on //
// I am out of the loop now does not get printed //
This is probably the easiest/lamest question.
So I am trying to initialize an array with values 0 to 3 increments of 0.25 in the viewDidLoad method, and I can see an infinite loop here.
NSArray *pickArray3 = [[NSMutableArray alloc] init];
int i = 0;
//for(i = 0.25; i<=3; i=i+0.25)
while (i<3)
{
//NSString *myString = [NSString stringWithFormat:@"%d", i];
i=i+0.25;
NSLog(@"The Value of i is %d", i );
//[pickArray3 addObject:myString]; // Add the string to the tableViewArray.
}
NSLog(@"I am out of the loop now");
self.doseAmount=pickArray3;
[pickArray3 release];
And This is the Output.
2011-06-01 11:49:30.089 Tab[9837:207] The Value of i is 0
2011-06-01 11:49:30.090 Tab[9837:207] The Value of i is 0
2011-06-01 11:49:30.091 Tab[9837:207] The Value of i is 0
2011-06-01 11:49:30.092 Tab[9837:207] The Value of i is 0
// And this goes on //
// I am out of the loop now does not get printed //
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的 i 是一个整数,它永远不会增加 0.25。使用浮点型或双精度型。
Your i is an integer, it will never increment adding 0.25. Use a float or double.
我是一个整数。因此
0+0.25 = 0
。i is an int. Therefore
0+0.25 = 0
.使用
float
而不是int
。因为每次表达式值 i+0.25 ==> (0+0.25)=> 0.25。
现在你将值 0.25 赋给整数因此它每次都会变成 0 并且
while
中的条件永远不会为 0 为假,所以它会进入无限循环< /强>。所以你的代码必须是
use
float
instead ofint
.because every time the expression value i+0.25 => (0 + 0.25 ) => 0.25.
Now you are assigning the value 0.25 to integer therefore it become 0 every time and condition in
while
will never be false with 0 , So it goes to infinite loop.So your code must be