Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 算术计算错误_Objective C - Fatal编程技术网

Objective c 算术计算错误

Objective c 算术计算错误,objective-c,Objective C,我试图从NSMutableArray计算2个检索到的值,但运行时出现错误,指向算术计算的方式 插入值的代码: 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]]; [d

我试图从
NSMutableArray
计算2个检索到的值,但运行时出现错误,指向算术计算的方式

插入值的代码:

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=((总分/次数)*100)的计算


Thanx提前…

trys
为0,因此
int numberoftrys=[[savedStock objectAtIndex:2]intValue]也将0分配给
numberofthries
,整数除以0会导致算术错误

考虑:

avg = (numberOfTries == 0) ? 0 : ((totalScore / numberOfTries) * 100);

trys
为0,因此
int numberoftrys=[[savedStock objectAtIndex:2]intValue]也将0分配给
numberofthries
,整数除以0会导致算术错误

考虑:

avg = (numberOfTries == 0) ? 0 : ((totalScore / numberOfTries) * 100);