私はプログラミングが初めてで、アプリを初めて試すときは、基本的な電卓を書いています。今のところ、4つの演算子(+、-、*、/)をすべて使用して計算させることができますが、2回目にequalsを押すとクラッシュします。計算を再度実行するにはどうすればよいですか?たとえば、「2 + 2 = 4」と入力した場合、2回目に等しいを押して「6」を生成し、3回目に押すと「8」を生成します。
これが私がこれまでに持っているものです。switchステートメントを使用しています。
-(void)equalsButton:(id)sender
{
second = [display.text integerValue];
int result;
NSArray* components;
switch (operator)
{
case 0:
components = [display.text componentsSeparatedByString:@"+"];
first = [(NSString*)[components objectAtIndex:0] integerValue];
second = [(NSString*)[components objectAtIndex:1] integerValue];
result = first + second;
break;
case 1:
components = [display.text componentsSeparatedByString:@"-"];
first = [(NSString*)[components objectAtIndex:0] integerValue];
second = [(NSString*)[components objectAtIndex:1] integerValue];
result = first - second;
break;
case 2:
components = [display.text componentsSeparatedByString:@"*"];
first = [(NSString*)[components objectAtIndex:0] integerValue];
second = [(NSString*)[components objectAtIndex:1] integerValue];
result = first * second;
break;
case 3:
components = [display.text componentsSeparatedByString:@"/"];
first = [(NSString*)[components objectAtIndex:0] integerValue];
second = [(NSString*)[components objectAtIndex:1] integerValue];
result = first / second;
break;
}
NSString * result1 = [NSString stringWithFormat:@"%i",result];
display.text = result1;
}