2011-07-04 6 views
0

저는이 문제를 하루 동안 알아 내려고 노력해 왔으며 수정 사항을 찾을 수없는 것 같습니다. 내가 할 노력하고있어목표 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; 
} 

이 버튼 다음 클릭하면 사람이 정수 값과 동일하다.

이 코드에 전혀 문제가 나타나지 않습니다.

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]; 
} 

} 

I 버튼 '이동'항상 사람의 가치를 가정을 클릭 할 때마다

문 경우 마지막이다.

아이디어가 있으십니까?

+0

당신이 '사용할 때 Xcode4 당신에게 = 경고를 표시합니다 '연산자를 호출합니다. 경고에 유의해야합니다. – Shreesh

+0

비교 연산자는 대입 연산자 (==)를 사용하고 대입 연산자 (=)는 사용하지 않아야합니다. – EmptyStack

+0

C에서 일부 수업을 듣고 ... 비교할 값을 지정하고 있습니다 !! – Aditya

답변

4

사용 == 아닌 사람이 하나의 동일 여부를 확인하는 것보다 사용자에게

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

그것은 할당 1과 비교한다. 1은 사람에게 할당되므로이 조건은 항상 true입니다. 대신 == 연산자가 필요합니다. 조건은 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) { 
//.. 
} 
//.. 
관련 문제