Objective c 计算返回0-c
我试图计算一个平均值,从一个文本字段中获取值,然后乘以标签的值Objective c 计算返回0-c,objective-c,c,uitextfield,int,uilabel,Objective C,C,Uitextfield,Int,Uilabel,我试图计算一个平均值,从一个文本字段中获取值,然后乘以标签的值 int grade1 = [[self.Cw1Grade text]intValue]; int grade1weight = self.weight1.text.intValue; int a1grade = grade1 / 100; int a1total = a1grade * grade1weight; NSString *grade1total = [NSString stringWi
int grade1 = [[self.Cw1Grade text]intValue];
int grade1weight = self.weight1.text.intValue;
int a1grade = grade1 / 100;
int a1total = a1grade * grade1weight;
NSString *grade1total = [NSString stringWithFormat:@"%d", a1total];
[self.averageLabel setText:grade1total];
谢谢你的时间你不能那样除法整数。或者,您可以这样做,但您不会得到预期的结果,因为整数除法返回整数(结果将向下舍入到下一个整数) 试试这个:
float a1grade = (float)grade1 / 100;
或
如果至少有一个操作数是浮点,则将使用浮点除法。当然,在这种情况下,您必须将结果存储在浮点变量中
另外,不要忘记,float的字符串格式说明符是
%f
,而不是%d
,这是一个标准的新手错误。当您对整数进行算术运算时,会得到一个向下截断的整数结果。所以96除以100,等于零。使用浮点。目标-C是C。学习C。哦,请在发布之前进行搜索。另请参阅
float a1grade = grade1 / 100.0;