2013-07-02 145 views
0

我有一段时间让这段代码正确摆脱。我目前被困在文本的最后一行。我是新来的编写代码。我学到的所有东西都可以在网上找到并通过这个网站找到。请参阅下面带下划线的注释。请帮帮忙,所以我可以看到,如果我的计算,即使作品...复杂计算

//.h 


@interface ViewController : UIViewController 

@property (weak, nonatomic) IBOutlet UITextField *Price87; 
@property (weak, nonatomic) IBOutlet UITextField *MPG87; 
@property (weak, nonatomic) IBOutlet UITextField *PriceE85; 
@property (weak, nonatomic) IBOutlet UITextField *MPGE85; 
@property (weak, nonatomic) IBOutlet UITextField *GasTankSize; 

- (IBAction)Settings:(id)sender; 
- (IBAction)pergallon:(id)sender; 
- (IBAction)pertank:(id)sender; 

@end 


//.m 

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController <----------- Getting a Incomplete Implementation here... 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)Settings:(id)sender { 
    Settings *settings = [[Settings alloc] initWithNibName:nil bundle:nil]; 
    [self presentViewController:settings animated:YES completion:NULL]; 
} 

- (IBAction)addNums:(id)sender { 
    int a = ([_Price87.text floatValue]); 
    int b = ([_MPG87.text floatValue]); 
    int c = ([_PriceE85.text floatValue]); 
    int d = ([_MPGE85.text floatValue]); 
    int e = ([_GasTankSize.text floatValue]); 
    int ans = ((a*e)-((e+(a*e)-(c*e)/b)*d)/e); 

    [ans setText:[NSString stringWithFormat:@"%i", pergallon]]; <--------- This is the line giving me trouble. I'm getting a "use of undeclaired identifier 'pergallon' 

} 

@end 
+0

'pergallon'是一种方法,不是addNums方法范围内的变量。 – CodaFi

回答

0

声明它,那么您已经一些错误,请考虑:

int a = ([_Price87.text floatValue]); 

在右手边的大小,你作为floatValue - 浮点与小数部分,但在左侧的大小你声明aint - 整数没有任何小数部分。该分配将截断舍弃小数部分的数字,例如, 1.82会变成1.这可能不是你想要的。您的意思是宣布afloat

但让我们看看你的数学和单位 - 根据你给你的领域的名称。

a大概是$/gal,e是加仑,所以a*e是在$(填满坦克的价格)。让我们把单位到您的公式的一部分:

e+(a*e) => gal + $ 

现在加入加仑,美元是没有意义的 - 你会得到一个号码,但它是一个废话号。表达式的其余部分也没什么意义。

编译器不会发现上述任何内容,计算机是快速白痴 - 要求他们计算废话,他们会这样做。

计算机会发现什么是简单的错误。它所投诉的行是指pergallon,它不存在。您可能打算使用ans。虽然解决这个问题可能会让编译器很高兴,但它不会解决你的单元问题,你需要弄清楚你的数学问题。

HTH。

0

你需要一个float或double数据类型声明变量,然后才能使用它。

尝试用

@property (nonatomic) float pergallon; 
+0

在该方法的范围内,这没有任何意义。他可能只是混淆了'ans'和一些名为'pergallon'的字段 – CodaFi