viewDidLoad 变成无限循环。帮助

发布于 2024-11-11 16:34:20 字数 890 浏览 4 评论 0原文

这可能是最简单/最蹩脚的问题。

因此,我尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

︶ ̄淡然 2024-11-18 16:34:20

你的 i 是一个整数,它永远不会增加 0.25。使用浮点型或双精度型。

**float** i = 0;
//for(i = 0.25; i<=3; i=i+0.25) 
while (i<3)
{ 
//NSString *myString = [NSString stringWithFormat:@"**%f**", i]; 
    i=i+0.25;
    NSLog(@"The Value of i is **%f**", i );
//[pickArray3 addObject:myString]; // Add the string to the tableViewArray.
 }

Your i is an integer, it will never increment adding 0.25. Use a float or double.

**float** i = 0;
//for(i = 0.25; i<=3; i=i+0.25) 
while (i<3)
{ 
//NSString *myString = [NSString stringWithFormat:@"**%f**", i]; 
    i=i+0.25;
    NSLog(@"The Value of i is **%f**", i );
//[pickArray3 addObject:myString]; // Add the string to the tableViewArray.
 }
千笙结 2024-11-18 16:34:20

我是一个整数。因此0+0.25 = 0

i is an int. Therefore 0+0.25 = 0.

神也荒唐 2024-11-18 16:34:20

使用 float 而不是 int

因为每次表达式值 i+0.25 ==> (0+0.25)=> 0.25。

i = i+0.25;

现在你将值 0.25 赋给整数因此它每次都会变成 0 并且 while 中的条件永远不会为 0 为假,所以它会进入无限循环< /强>。

所以你的代码必须是

float 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 %f", i );
//[pickArray3 addObject:myString]; // Add the string to the tableViewArray.
 }

use float instead of int .

because every time the expression value i+0.25 => (0 + 0.25 ) => 0.25.

i = i+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

float 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 %f", i );
//[pickArray3 addObject:myString]; // Add the string to the tableViewArray.
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文