2011-07-04 18 views
0

私は今、この問題を1日分見つけようとしていますが、修正が見つからないようです。目的C if文が正常に動作しない

int person; 



-(IBAction)personOne:(id)sender{ 
[twoPerson stopAnimating]; 
[fourPerson stopAnimating]; 
[threePerson stopAnimating]; 
[onePerson startAnimating]; 

person = 1; 

} 

-(IBAction)personTwo:(id)sender{ 
[threePerson stopAnimating]; 
[fourPerson stopAnimating]; 
[onePerson startAnimating]; 
[twoPerson startAnimating]; 

person = 2; 
} 
-(IBAction)personThree:(id)sender{ 
[fourPerson stopAnimating]; 
[onePerson startAnimating]; 
[twoPerson startAnimating]; 
[threePerson startAnimating]; 

person = 3; 
} 
-(IBAction)personFour:(id)sender{ 
[onePerson startAnimating]; 
[twoPerson startAnimating]; 
[threePerson startAnimating]; 
[fourPerson startAnimating]; 

person = 4; 
} 

私がしようとしていることは、このボタンをクリックしてからPersonが整数値に等しい場合です。

このコードには何も問題はありません。

次に、if文を使用している別のボタンがあります。

-(IBAction)go:(id)sender{ 
ammountDueFloat = ([ammountDue.text floatValue]); 
tipPercent1; 
findAmmount = tipPercent1 * ammountDueFloat; 
NSString *showAmmountText = [[NSString alloc]initWithFormat:@"$%.2f", findAmmount]; 
showammount.text = showAmmountText; 



if (person = 1) { 
    showPerPerson = findAmmount/1; 
    perPersonLabel.text = [[NSString alloc]initWithFormat:@"$%.2f",  showPerPerson]; 
} 
if (person = 2) { 
    showPerPerson = findAmmount/2; 
    perPersonLabel.text = [[NSString alloc]initWithFormat:@"$%.2f", showPerPerson]; 
} 
if (person = 3) { 
    showPerPerson = findAmmount/3; 
    perPersonLabel.text = [[NSString alloc]initWithFormat:@"$%.2f", showPerPerson]; 
} 
if (person = 4) { 
    showPerPerson = findAmmount/4; 
    perPersonLabel.text = [[NSString alloc]initWithFormat:@"$%.2f", showPerPerson]; 
} 
else { 
    showPerPerson = findAmmount/1; 
    perPersonLabel.text = [[NSString alloc]initWithFormat:@"$%.2f", showPerPerson]; 
} 

} 

「go」ボタンをクリックするたびに、常にpersonの値が最後のif文であるとみなされます。

アイデア?

+0

あなたは=」を使用する場合Xcode4はあなたに警告を与えます'演算子をif()条件で使用します。あなたは警告に留意する必要があります。 – Shreesh

+0

比較のために、等号演算子(==)、代入演算子(=)を使用しないでください。 – EmptyStack

+0

Cでいくつかのクラスを履修してください...あなたは比較したいところに値を割り当てています!! – Aditya

答えて

4

使用は==

if(person == 1) 
{ 
//... 
} 
2
person = 1 

を比較することはなく、人が1に等しいかどうかをチェックするよりも、人に1を代入します。人に1が割り当てられるので、この条件は常に真です。 の代わりに==の演算子がifの条件で必要です。

if (person == 1) { 

} else if (person == 2) { 

} // similar 
2

私はコメントとしてそれを書くだろうが、それは便利ではないですが、それは両方の答えが示唆したように、あなたは等価演算子を使用する必要があり、ここで

ですので、ご検討ください。

if ((person < 1) || (person > 4) { 
    person = 1; 
} 
showPerPerson = findAmmount/person; 
perPersonLabel.text = [[NSString alloc]initWithFormat:@"$%.2f",  showPerPerson]; 

代わりに場合の全体の。

そして、あなたがもし例を使用している場合、あなたはそれ以外の場合は最後の部分は毎回の人が実行される、それぞれのケースの後に他の追加すべき= 4:!

if (person == 1) { 
//.. 
} 
else if (person == 2) { 
//.. 
} 
else if (person == 3) { 
//.. 
} 
//.. 
関連する問題