我正在使用Xcode中的应用程序并遇到了问题。 我的应用程序需要从TextField(用户输入信息的地方)收集输入,并将其放入我的等式&最终给用户一个结果。
例如 - a 用户输入他们的体重,身高和年龄。
我的应用程序需要接受这些输入并将它们放入以下等式中:
Men: RMR = 66,473 + (13,751*WEIGHT) + (5,0033*HEIGHT) - (6,755*AGE)
但我该怎么编码呢?到目前为止我所做的:
我的.h文件如下:
#import <UIKit/UIKit.h>
IBOutlet UITextField *Weight;
IBOutlet UITextField *Height;
IBOutlet UITextField *Age;
IBOutlet UILabel *rectResult;
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *Weight;
@property (weak, nonatomic) IBOutlet UITextField *Height;
@property (weak, nonatomic) IBOutlet UITextField *Age;
@property (weak, nonatomic) IBOutlet UILabel *rectResult;
-(IBAction)calculate:(id)sender;
@end
和我的.m文件:
@implementation ViewController
-(IBAction)calculate:(id)sender {
float floatRectResult=[Weight.text floatValue]*
[Age.text floatValue];
NSString *stringRectResult=[[NSString alloc]
initWithFormat:@"%1.2f",floatRectResult];
rectResult.text=stringRectResult;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
PS:对不起我的英语 - 有人可以帮帮我吗? :)
答案 0 :(得分:1)
尝试这样的事情:
-(IBAction)calculate:(id)sender {
//66,473 + (13,751*WEIGHT) + (5,0033*HEIGHT) - (6,755*AGE)
float result = (66473 + (13751*[Weight.text floatValue]) + (50033*[Height.text floatValue]) - (6755*[Age.text floatValue]));
rectResult.text = [NSString stringWithFormat:@"%.2f", result];
}
由于您似乎正在使用某些常量,因此#define
可能更好,以便您以后可以轻松更改它们。例如:
//Above your implementation
#define kWeightConst 13751.0 //Can be used later as just kWeightConst