【问题标题】:+/- and root buttons in a calculator app计算器应用程序中的 +/- 和根按钮
【发布时间】:2012-08-27 10:35:24
【问题描述】:

我正在尝试制作一个计算器应用程序,有人建议使用以下代码进行基本操作(加法、减法...);

         if ([operand isEqualToString:@"+"])
           lastKnownValue += currentValue;

    else if ([operand isEqualToString:@"-"])
           lastKnownValue -= currentValue;

    else if ([operand isEqualToString:@"X"])
           lastKnownValue *= currentValue;

    else if ([operand isEqualToString:@"/"])

在哪里

         lastKnownValue 

用于在使用操作数之前保存输入的数字

         currentValue

在屏幕上显示 lastKnownValue。
(操作是单独的按钮。)

它确实有效,但我如何制作不需要输入第二个数字或点击“EQUALS”按钮的操作按钮?

【问题讨论】:

  • 请为您的代码提供更多上下文。它是在按钮上执行的还是什么? lastKnownValue 和 currentValue 是什么?

标签: objective-c ios cocoa-touch calculator


【解决方案1】:

您对 currentvalue 进行操作,然后用结果更新 currentValue,并刷新显示。将 lastknownValue 设置为 nil。

编辑:

self.currentvalue = sqrt(current value);
self.lastknownValue = nil
[self.view setNeedsDisplay];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多