-1

如果在文本字段中输入的文本与数组中的对象匹配,我正在尝试制作一个应用程序,该应用程序可以将整数带走或加 1。

我的 .m 文件中的代码

NSString *inputtwo =EnterNameText.text;
BOOL isItright = NO;
for(NSString *possible in scoreArray1)
{
    if([inputtwo isEqual:possible] )
    {
        isItright = YES;
        break;
    }
}

NSString *wronginput = EnterNameText.text;
BOOL isWrong = NO;
for(NSString *wrong in scoreArray1)
{
    if(![wronginput isEqual:wrong ] )
    {
        isWrong = YES;
        break;
    }
}

static int myInt;

if(isItright)
{
    myInt++;

    NSString *score = [NSString stringWithFormat:@"%d", myInt];
    [scorelabel setText:score];
}

if (isWrong)
{
    myInt--;

    NSString *score = [NSString stringWithFormat:@"%d", myInt];
    [scorelabel setText:score];
}

因此程序会检查名为 scoreArray1 的数组中是否存在匹配项,如果存在则将 myInt 加 1,否则将带走一个。

问题是它只是拿走一个,不管它是对还是错。

谢谢你的时间。

4

3 回答 3

2

isEqualToString如果您正在比较字符串值,则应该使用。该isEqual方法通常比较指针值,因此您从文本字段中获取的内容与输入到数组中的内容总是会返回不同的值。

于 2012-10-22T20:50:21.400 回答
0

您的程序中有逻辑错误。首先,您检查文本字段的内容是否与您的任何元素匹配scorearray1,如果匹配isItright为真。到此为止,一切都是正确的(除了相等性检查最好用 来完成isEqualToString)。但是现在您检查是否scorearray1不包含您的文本字段的内容,并且如果只有一个元素scorearray1不匹配,则文本字段isWrong将为真。

您应该只使用第一个循环和以下if else. 如果 textfield 的内容等于scorearray1add 1 to中的任何字符串myInt,否则(数组中没有匹配项)减去 1。

使用以下代码:

NSString *inputtwo =EnterNameText.text;
BOOL isItright = NO;
for(NSString *possible in scoreArray1)
{
    if([inputtwo isEqualToString:possible] )
    {
        isItright = YES;
        break;
    }
}

static int myInt;

if(isItright)
{
    myInt++;
}
else
{
    myInt--;
}
NSString *score = [NSString stringWithFormat:@"%d", myInt];
[scorelabel setText:score];
于 2012-10-22T21:05:59.760 回答
0
NSString *input = EnterNameText.text;
BOOL matchFound = NO;
static in myInt;

for (NSString *score in scoreArray1)
    if ([input isEqualToString:score])
    {
        matchFound = YES;
        break;
    }

if (matchFound)
    myInt++;
else
    myInt--;
于 2012-10-22T21:35:49.653 回答