存储用户输入,最后计算并显示

发布于 2024-12-03 14:23:05 字数 1733 浏览 1 评论 0原文

我正在编写一个程序来计算用户提供的输入,简单的数字。 2、4、7、10、12 等等。

我需要程序以某种方式存储这些输入(来自不同的文本字段)。在内部方程中使用它们,然后最终将它们展示给用户。

我该怎么做?请注意,我在编程方面相当新手。

我知道如何定义 textFields,执行计算 IBAction ,并且我尝试创建一个 -(void)calculate ,它可以工作,但仅适用于 1 个输入。当我执行以下代码时,输​​入来自 6 个文本字段而不是 3 个(在第一行 (resultOne) 中使用),它给出了错误的数字。但我仍然需要一种更好的方法来做到这一点。

-(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];
}

我知道上面的代码输出 resultOne,但这只是为了我自己的测试,以了解该方程是否有效。但我真的很想重新开始,因为我不相信上面的代码适合我需要它做的事情。

请记住,我是一个初学者,请多解释一下,而不仅仅是向我展示一些代码。

我期待您能给我任何帮助。

编辑! - 下面是新代码

-(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) / (weightOne*2 + weightTwo*2);

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

}

我决定更改我的代码,以便轻松计算输入。现在我有另一个问题。

是否可以以某种方式保存这个方程,这样我就可以多次重复使用它,而不必一遍又一遍地输入代码。

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) / (weightOne*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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心荒芜 2024-12-10 14:23:05

首先计算的是您的 IBAction 方法,点击按钮时将调用该方法。因此,将原型更改为 -(IBAction)calculate:(id)sender

接下来,如果您能够为文本字段提供出口,那么在其中输入文本就没有坏处,也绝对没有困难。

您可以做的几件事就是验证输入是否正确。 [v1Text.text floatValue] 是从文本中获取数字的正确表达式。您还可以查看intvalue

现在,完成计算后,您可以使用对要显示结果的标签/文本字段的引用。

您可以按照在代码中所做的方式完全更改该标签。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文