【发布时间】:2010-05-12 20:58:03
【问题描述】:
作为一种练习,我正在开发一个应用程序来解决著名的中学勾股定理,a squared + b squared = c squared。不幸的是,在我看来,即将到来的答案与实际答案无关。这是“解决”操作期间使用的代码。
- (IBAction)solve {
int legoneint;
int legtwoint;
int hypotenuseint;
int lonesq = legoneint * legoneint;
int ltwosq = legtwoint * legtwoint;
int hyposq = hypotenuseint * hypotenuseint;
hyposq = lonesq + ltwosq;
if ([legone.text isEqual:@""]) {
legtwoint = [legtwo.text intValue];
hypotenuseint = [hypotenuse.text intValue];
answer.text = [NSString stringWithFormat:@"%d", legoneint];
self.view.backgroundColor = [UIColor blackColor];
}
if ([legtwo.text isEqual:@""]) {
legoneint = [legone.text intValue];
hypotenuseint = [hypotenuse.text intValue];
answer.text = [NSString stringWithFormat:@"%d", legtwoint];
self.view.backgroundColor = [UIColor blackColor];
}
if ([hypotenuse.text isEqual:@""]) {
legoneint = [legone.text intValue];
legtwoint = [legtwo.text intValue];
answer.text = [NSString stringWithFormat:@"%d", hypotenuseint];
self.view.backgroundColor = [UIColor blackColor];
}
}
顺便说一下,legone, legtwo, and hypotenuse 都表示对应于直角三角形每个数学部分的UITextField。 Answer 是 UILabel,它告诉你答案,你猜对了。有没有人看到程序中的任何缺陷?提前致谢!
【问题讨论】:
标签: iphone math pythagorean