문제

I'm trying to write code that detects if an integer is greater than another integer. Is this possible?

Here is what i've done so far.

if (NumCorrect >> NumWrong) {
    btnCool.title = @"Awww";
}

else {
    btnCool.title = @"Cool!";
}

All its doing is going to the else

EDIT:

NSString *numCorrect = [NSString stringWithFormat:@"%d",NumCorrect];
NSString *numWrong = [NSString stringWithFormat:@"%d", NumWrong];
lblWrong.text = numWrong;
lblCorrect.text = numCorrect;
if (NumCorrect > NumWrong) { 
    btnCool.title = @"Awww"; 
} else { 
    btnCool.title = @"Cool!"; 
}
도움이 되었습니까?

해결책

Use single >

if (NumCorrect > NumWrong) {
    btnCool.title = @"Awww";
} else {
    btnCool.title = @"Cool!";
}

Double >> is a bit shift operation. You shift every bit in the binary representation of your variable NumCorrect NumWrong amount of bytes to the right. In almost all cases this will return in a number other then 0, which will then treated as a false value and thus the else block is executed.

다른 팁

Almost perfect - just take off one of those >'s. >> and << are for "bit-shifting", a weird hold-over from the earliest days of programming. You're not gonna use them much. What you really want is > and <, which is for testing if numbers are greater than each other or less than each other.

In addition, you may remember from math class that ≥ and ≤ (greater-than-or-equal-to and less-than-or-equal-to) are useful operations as well. Because there's no symbols for those on most keyboards, however, C and Xcode use >= and <= instead.

Finally, you may already know this, but to check if two numbers are exactly equal to each other you can use == (because = is used for setting the contents of variables).

Hope that's helpful!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top