阵列发行目标C X $ C $Ç阵列、目标

2023-09-11 23:29:19 作者:∕那季櫻花雨∕

我有一个问题,我的算法计算疼。用户输入一个字到的UITextField,并且如果单词的字符串阵列中相匹配(@道)中的int'得分'将由1加

I have a problem with my algorithm for calculating a sore. The user enters a word into the UITextField, and if the word matches a string in the array (@"The Word") the int 'score' will be added by 1.

然后INT得分为用户获得一个词正确的设置为标签。 (显示SCORE)

Then the int score is set as a label as the user gets a word right. (DISPLAYING THE SCORE)

这个问题,用户可以只保留上输入相同的文字一遍又一遍的比分将保持一个持续上升。是否有知道,如果一个字已经输入,所以只能用这个词一次命令。

THE PROBLEM, a user can just keep on entering the same word over and over again and the score will keep going up by one. IS there a command for knowing if a word has already been entered, so you can only use the word once.

在code

 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];

}

更新!!!!!!

UPDATE!!!!!!

    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];

}

不过现在的APP是不行的,它崩溃了,有什么建​​议?

HOWEVER NOW THE APP DOES NOT WORK, IT CRASHES, any suggestions?

推荐答案

其实,如果你有一个法律字样的数组,要走的路是简单地删除每一个字,因为它叫出来,直到阵列到达零项长。

Actually, if you have an array of "legal" words, the way to go is to simply remove each word as it's called out, until the array gets to be zero entries long.

NSMutableArray* scoreArrayCopy = [NSMutableArray arrayWithArray:scoreArray];
int originalCount = scoreArrayCopy.count;
...
while (scoreArrayCopy.count > 0) {
   NSString* guess = <get next guess>;
   [scoreArrayCopy removeObject:guess];
   score = originalCount - scoreArrayCopy.count;
}

(如果你有很多的话事情会如果使用了的NSMutableSet,而不是一个NSMutableArray更有效率,但逻辑将是相同的。)

(If you have a lot of words things would be more efficient if you used an NSMutableSet instead of an NSMutableArray, but the logic would be the same.)