开发者

Store user input, calculate and show in the end

开发者 https://www.devze.com 2023-04-03 13:33 出处:网络
I\'m writing a program that calculates input a user gives, simple numbers. 2, 4, 7, 10, 12 and so on.

I'm writing a program that calculates input a user gives, simple numbers. 2, 4, 7, 10, 12 and so on.

I need the program to store these inputs (that come in from different textfields) somehow. Use them in an internal equation, and then show them to the user in the end.

How do I do this? Please take note that I'm rather new at programming.

I know how to define textFields, do a calculate IBAction , and I've tried creating a -(void)calculate, which works, but only on 1 input. When I execute the following code, with input from 6 textfields instead of 3 (which are used in the first line (resultOne)) it gives me the wrong numbers. But I still need a better way of doing this.

-(void) calculate{
weightOne = [v1Text.text floatValue]/2; 
weightTwo = [v2Text.text floatValue]/2;
resultOne = [m1Text.text floatValue] * weightOne + [s1Text.text floatValue] * weightOne;
resultTwo = [m2Text.text floatValue] * weightTwo + [s2Text.text floatValue] * weightTwo;
_resultTotal = (resultOne + resultTwo) / (weightOne + weightTwo);

NSString *resultString = [[NSString alloc] 
                          initWithFormat: @"%.2f", resultOne];
resultLabel.text = resultString;
[resultString release];
}

I know that the above code outputs resultOne, but this is just for my own testing, to know that the equation works. But I would really like to start all fresh, since I don't believe that the above code is suitable for what I need it to do.

Please bare in mind that I am a beginner, and explain a little more, than just showing me some code.

I look forward to any help you can give me.

EDIT! - BELOW IS NEW CODE

-(IBAction) calculate:(id)sender {
weightOne = 1; 
weightTwo = 0.75;
weightThree = 0.50;

mOne = [m1Text.text floatValue];
mTwo = [m2Text.text floatValue];
sOne = [s1Text.text floatValue];
sTwo = [s2Text.text floatValue];

resultOne = (mOne*weightOne) + (sOne*weightOne);
resultTwo = (mTwo*weightTwo) + (sTwo*weightTwo);

_resultTotal = (resultOne + resultTwo) / (weightOn开发者_JAVA技巧e*2 + weightTwo*2);

NSString *resultString = [[NSString alloc] 
                          initWithFormat: @"Gennemsnit %.2f", _resultTotal];
resultLabel.text = resultString;

}

I decided to change my code, in order to easily calculate the input. Now I have another question.

Is it possible to save this equation in some way, so I can reuse it as many times as I want, without having to type the code again and again and again.


First of all calculate can be your IBAction method which will be called when tapped on the button. So change the prototype to -(IBAction)calculate:(id)sender.

Next if you are able to have outlets for the text fields, then there is no harm and absoulutely no difficulty in getting the text entered in them.

Few things you can do are to validate for correct input. [v1Text.text floatValue] is right expression for getting the numbers from text. You can also have a look at intvalue.

Now once you have done your computation, you can use the reference to the label/textfield where you want to display the result.

You can change that label exactly the way you did in your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号