2013-11-28 62 views
-2

对不起,这是一个愚蠢的问题。如果x> 0但小于y

我使用UITextField,我可以输入一些数字。

不,我用这个代码来检测,如果输入的号码是0,大于0和小于15

if (myNr <= 15){ 
NSLog (@"the number is OK"); 

} 
else if (myNr > 15) 
{ 
NSLog(@"this number doesn't existing"); 
} 
else if (myNr == 0) 
{ 
NSLog(@"number should be between 1 and 15"); 
} 

我得到了一些错误,当数为0

我需要只能插入1到15之间的数字,如果数字是0或大于15,NSLog应该说。

谢谢

+0

基本上,你的代码确实工作,因为0 <= 15是真的,并且第一个语句首先执行,因此你得到的数字是OK的...... – inixsoftware

回答

0

你应该把if(myNr == 0)在第一

if (myNr == 0) 
{ 
NSLog(@"number should be between 1 and 15"); 
} else if (myNr <= 15) 
{ 
    NSLog (@"the number is OK"); 

} 
else if (myNr > 15) 
{ 
    NSLog(@"this number doesn't existing"); 
} 
+0

...谢谢,作品..等待8分钟来标记答案。 – user2805816

+0

@ user2805816让我知道你是否有任何问题 – chancyWu

0

你会得到什么错误?

不好意思,请记住:大写字母很重要。 NSLognslog不同。

0

您的代码简化为:

if (myNr <= 15) 
{ 
    NSLog(@"the number is OK"); 
} 
else 
{ 
    NSLog(@"this number doesn't existing"); 
} 

为0的情况下从未达到。

记住测试是按顺序考虑的。