将 int 赋值给新 int
到目前为止,我有代码
- (void)CreatenewBlock:(int)blockCountToSpawn
{
for (int i=0; i != blockCountToSpawn; i++)
{
theBlockxCord = arc4random() % 4;
theBlockyCord = arc4random() % 4;
NSLog(@"New Block with Cords (%i, %i)",theBlockxCord, theBlockyCord);
}
}
This 循环直到达到 blockCountToSpawn 的数量 这完全按照我想要的方式工作,但我想每次将 theBlockxCord 设置为一个新变量。所以最终结果会是这样的:
theBlockxCordOfBlock1=2
theBlockxCordOfBlock2=4
theBlockxCordOfBlock3=1
theBlockxCordOfBlock4=3
而不是每次都覆盖 theBlockXCord。
对于奖励积分,是否有一种方法可以在数组中调用它们,这样我就不必继续这样做:
if (theBlockxCordOfBlock1 == 2 || theBlockxCordOfBlock3 == 2 ..etc)
{
do stuff..
}
So far I have the code
- (void)CreatenewBlock:(int)blockCountToSpawn
{
for (int i=0; i != blockCountToSpawn; i++)
{
theBlockxCord = arc4random() % 4;
theBlockyCord = arc4random() % 4;
NSLog(@"New Block with Cords (%i, %i)",theBlockxCord, theBlockyCord);
}
}
This loops until it reaches the anount of blockCountToSpawn
This works exactly how I want it to but I want to set theBlockxCord to a new variable each time. So the end result would be something like:
theBlockxCordOfBlock1=2
theBlockxCordOfBlock2=4
theBlockxCordOfBlock3=1
theBlockxCordOfBlock4=3
Instead of theBlockXCord being overwritten each time.
For bonus points, Is there a single way to call of them maybe in an array so I don't have to keep doing this:
if (theBlockxCordOfBlock1 == 2 || theBlockxCordOfBlock3 == 2 ..etc)
{
do stuff..
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 C 数组或 NSMutableArray。您必须先将
int
转换为NSNumber
,然后才能将其添加到数组中。如果您想搜索
2
,请执行此操作或
编辑
You can use C arrays or an NSMutableArray. You will have to convert the
int
s toNSNumber
s before you can add it to the array.if you want to search for
2
then do thisor
EDIT
对于第一个问题:将结果放入传统数组(就像任何 C 程序一样)或 NSMutableArray 中。 http://开发人员。 apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html
对于您的第二个问题:如果您使用 NSMutableArray 来存储对象(例如 NSNumber),您可以调用
containsObject:
来确定对象是否在数组中。 http://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/containsObject:For your first question: Put your results in either a conventional array just like any C program or in an NSMutableArray. http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html
For your second question: If you used an NSMutableArray to store objects (say, an NSNumber), you can call
containsObject:
to determine if an object is in the array. http://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/containsObject: