0

取得した 2 つの値を計算しようとしてNSMutableArrayいますが、実行時に算術計算が行われた方法を指すエラーが発生しました。

値を挿入するコード:

NSString *lastEx = @"None";
int lastScore = 0;
int tries = 0;
int total_score = 0;
int avg = 0;
int credit = 100;

[data addObject:lastEx];
[data addObject:[NSNumber numberWithInt:lastScore]];
[data addObject:[NSNumber numberWithInt:tries]];
[data addObject:[NSNumber numberWithInt:total_score]];
[data addObject:[NSNumber numberWithInt:avg]];
[data addObject:[NSNumber numberWithInt:credit]];

取得するコード:

NSMutableArray *savedStock = [[NSMutableArray alloc] initWithContentsOfFile:path];

previousExercise.text = [savedStock objectAtIndex:0];

NSString *lastScoring = [NSString stringWithFormat:@"%d", [[savedStock objectAtIndex:1] intValue]];
previousScore.text = lastScoring;

NSString *totalTries = [NSString stringWithFormat:@"%d", [[savedStock objectAtIndex:2] intValue]];
attempts.text = totalTries;

NSString *average = [NSString stringWithFormat:@"%d", [[savedStock objectAtIndex:4] intValue]];
avgScore.text = average;

int totalScore = [[savedStock objectAtIndex:3] intValue];
int numberOfTries = [[savedStock objectAtIndex:2] intValue];
int avg;

avg = ((totalScore / numberOfTries) * 100);

NSString *rem_credit = [NSString stringWithFormat:@"%d", [[savedStock objectAtIndex:5] intValue]];
credits.text = rem_credit;

エラーは=>の計算ですavg = ((totalScore / numberOfTries) * 100);

事前にサンクス...

4

1 に答える 1

4

triesは 0 なので、 にもint numberOfTries = [[savedStock objectAtIndex:2] intValue];0 を代入しnumberOfTries、整数を 0 で除算すると算術エラーが発生します。

検討:

avg = (numberOfTries == 0) ? 0 : ((totalScore / numberOfTries) * 100);
于 2012-05-07T01:27:12.337 に答える