2012-03-02 161 views
-1

對於我的英文不好,我表示歉意。密鑰輸入驗證

你怎麼能這樣 「textFiled1.text」 檢查低於10乘以5,如果乘以20

-(IBAction)calculate 
{ 
float x = ([textFiled1.text floatValue]); 

float c = x*10; 

lable.text = [[NSString alloc] initWithFormat:@"%2.f", c]; 

} 
+0

如果x小於10,您是否想將x的值乘以5,否則乘以20?如果x等於10,該怎麼辦? – sch 2012-03-02 20:50:04

回答

0
-(IBAction)calculate 
{ 
    float x = [textFiled1.text floatValue]; 

    float c = 0; 

    if (x <= 10) { // Or < instead of <= 
     c = x * 5; 
    else { 
     c = x * 20; 
    } 

    lable.text = [[NSString alloc] initWithFormat:@"%2.f", c]; 
} 

或者在一個以上的線路10:

float c = (x <= 10) ? x * 5 : x * 20; // Or < instead of <= 
0
float c; 

if (x < 10) { 
    c = x*10; 
} else if (x >10) { 
    c = x*20; 
}