我的计算疮的算法有问题。用户在 UITextField 中输入一个单词,如果该单词与数组中的字符串 (@"The Word") 匹配,则 int 'score' 将加 1。
然后 int 分数被设置为标签,因为用户得到了正确的单词。(显示分数)
问题是,用户可以一遍又一遍地输入同一个单词,分数会不断上升。是否有一个命令可以知道是否已经输入了一个单词,所以你只能使用这个单词一次。
编码
NSArray *scoreArray1 = [NSArray arrayWithObjects:
@"Word 1", @"Word 2", @"Word 3", nil];
NSString *inputtwo =_EnterNameText.text;
BOOL isItright = NO;
for(NSString *possible in scoreArray1) {
if([inputtwo isEqual:possible] ) {
isItright = YES;
break;
}
}
if(isItright) {
static int myInt = 0;
myInt++;
NSString *score = [NSString stringWithFormat:@"%d", myInt];
[_scorelabel setText:score];
}
更新!!!!!!
NSArray *scoreArray1 = [NSArray arrayWithObjects:
@"Alan Shearer", @"Shearer", @"Andrew Cole", @"Andy Cole", @"Cole", @"Thierry Henry", @"Henry", @"Robbie Fowler", @"Fowler", @"Frank Lampard", @"Lampard", @"Michael Owen", @"Owen", nil];
NSSet *set2 = [NSSet setWithArray:scoreArray1];
NSString *inputtwo =_EnterNameText.text;
BOOL isItright = NO;
for(NSString *possible in set2) {
if([inputtwo isEqual:possible] ) {
isItright = YES;
break;
}
}
if(isItright) {
static int myInt = 0;
myInt++;
NSString *score = [NSString stringWithFormat:@"%d", myInt];
[_scorelabel setText:score];
}
但是现在该应用程序无法正常工作,它崩溃了,有什么建议吗?