1

我正在构建一个执行大量数学和计算的应用程序。我有显示在标签中的滑块值,我必须在函数中使用这些值来获得最终值。我有这些滑块值(这些数字是我作为用户输入来测试公式并查看它是否有效):

Ageslider=20, weightslider=144, feetslider=5,inchesslider=9.03.

我必须首先将重量、英尺和英寸转换为公制以插入整体公式,但用户不需要查看这些值。将这些转换为公制,公式如下:

weightslider/2.2,  feetslider*12*2.54,   inchesslider*2.54.

现在对于整体公式:

66.473+5.0033*((feetslider*12.2.54)+(inchesslider*2.54))+13.7516*(weightslider/2.2)-6.755*ageslider

或简化这是原始公式应该是什么:

66.473+5.0033*(height in cm)+13.7516*(weight in kg)-6.755*age

当我输入公式时

(66.473+5.0033*((feetslider*12.2.54)+(inchesslider*2.54))+13.7516*(weightslider/2.2)-6.755*ageslider)

然后运行程序并使用程序中的测试数字我无法得到最终值等于它应该的值。我已将公式放入 excel 中,并以正确的操作顺序通过我的计算器运行它并得到1708.7373. 当我在程序中运行它时,它会出现1747.7.

然后我尝试将 (float) 放在公式中滑块值的前面,程序得到了这个1718.67:然后我尝试将 (double) 放在滑块值的前面,程序得到了这个:1689.62. 这是我正在使用的从 xcode 中复制的实际代码:

label.text = [[NSString alloc] initWithFormat:@"%.2f",66.473+(5.0033*((feetslider.value*12*2.54)+(inchesslider.value*2.54)))+(13.7516*(weightslider.value/2.2))-(6.755*ageslider.value)];

它还提出了警告:Assignment 从整数中生成指针而不进行强制转换。我不知道那个是什么意思。

有人请帮助我,我已经坚持了几个小时试图改变公式以获得正确的答案并摆脱警告,但我无法终生得到它!

谢谢!

更新!!这是我现在拥有的确切代码,但仍然没有给出正确的答案。当我运行程序时,我仍然收到错误“赋值从整数中生成指针而不进行强制转换”。它显示在“if(malebutton = Yes).

-(IBAction)malebutton{ if (malebutton = YES) {

    //Intermediate values
    float totalWeight;
    totalWeight = weightslider.value*.45454545;
    float heightFeetToCM;
    heightFeetToCM =feetslider.value*12*2.54;
    float heightInchesToCM;
    heightInchesToCM = inchesslider.value*2.54;
    float totalHeight; 
    totalHeight = heightFeetToCM + heightInchesToCM;

    //Final Component Values
    float calculatedHeight;
    calculatedHeight = 5.0033*totalHeight;
    float calculatedWeight;
    calculatedWeight = 13.7516*totalWeight;
    float calculatedAge;
    calculatedAge = 6.755*ageslider.value;

    //Total Value
    float finalValue;
    finalValue = 66.473 + calculatedHeight + calculatedWeight - calculatedAge;

    totalcaloriesburnedlabel.text = [[NSString alloc] initWithFormat:@"%.2f",finalValue];

更新 2;

这是我将滑块连接到其标签的方式。它们的格式都是相同的,除了我在每个上显示了多少个小数。

-(IBAction)changeLabelFeet { feetLabel.text = [[NSString alloc] initWithFormat: @"%.0f",feetSlider.value]; }

4

1 回答 1

0

从原始答案更新:我回去自己在 Xcode 上尝试过。我开始只写值(浮点年龄 = 20;),看看问题是否出在计算中,最后得到正确的答案,即 1708.74(字符串中的浮点数设置为 .2f)。然后我决定看看从滑块中获取值是否是问题,从滑块上的年龄开始,其他所有内容仍然是硬编码的,我最初得到了错误的答案并很快意识到我没有在 IB 中连接我的滑块. 更正后,我得到了正确的答案。这是我的代码。

。H

@interface ViewController : UIViewController {

IBOutlet UILabel *myLabel;
IBOutlet UISlider *ageSlider;
IBOutlet UISlider *weightSlider;
IBOutlet UISlider *feetSlider;
IBOutlet UISlider *inchesSlider;


}

@property (nonatomic, retain) IBOutlet UILabel *myLabel;
@property (nonatomic, retain) IBOutlet UISlider *ageSlider;
@property (nonatomic, retain) IBOutlet UISlider *weightSlider;
@property (nonatomic, retain) IBOutlet UISlider *feetSlider;
@property (nonatomic, retain) IBOutlet UISlider *inchesSlider;

@end

.m

@interface ViewController ()

@end

@implementation ViewController

@synthesize myLabel;
@synthesize ageSlider;
@synthesize weightSlider;
@synthesize feetSlider;
@synthesize inchesSlider;

-(void)calculateLabel{

//float age = 20;
float age = ageSlider.value;
NSLog(@"My age slider value is %.2f", ageSlider.value);
//float weight = 144;
float weight = weightSlider.value;
NSLog(@"My weight slider value is %.2f", weightSlider.value);
//float heightFeet = 5;
float heightFeet = feetSlider.value;
NSLog(@"My feet slider value is %.2f", feetSlider.value);
//float heightInches = 9.03;
float heightInches = inchesSlider.value;
NSLog(@"My inches slider value is %.2f", inchesSlider.value);


float weightKG = weight*.454545;
float heightFeetToCM = heightFeet * 12 * 2.54;
float heightInchesToCM = heightInches * 2.54;
float totalHeight = heightFeetToCM + heightInchesToCM;

float calculatedHeight = 5.0033 * totalHeight;
float calculatedWeight = 13.7516 * weightKG;
float calculatedAge = 6.755 * age;

float finalValue = 66.473 + calculatedHeight + calculatedWeight - calculatedAge;

myLabel.text = [NSString stringWithFormat:@"%.2f", finalValue];

}

- (void)viewDidLoad
{
    [super viewDidLoad];


    [self calculateLabel];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

所以首先,检查以确保你的 NSLog 看起来像这样......

2012-06-17 23:57:53.557 Testing6[1404:10103] 我的年龄滑块值为 20.00

2012-06-17 23:57:53.559 Testing6[1404:10103] 我的体重滑块值为 144.00

2012-06-17 23:57:53.559 Testing6[1404:10103] 我的脚滑块值为 5.00

2012-06-17 23:57:53.559 Testing6[1404:10103] 我的英寸滑块值为 9.03

如果在那之后它不起作用,我唯一能想到的另一件事是,您的滑块可能不会连续更新(也就是显示滑块值的标签向用户显示年龄为 20,但是它真的设置为19)。如果我深入研究我的旧 Xcode 项目,我可能会为您提供帮助。让我知道。

于 2012-06-16T04:30:33.120 回答