gpt4 book ai didi

ios - 计算器除以零

转载 作者:行者123 更新时间:2023-11-28 18:23:26 24 4
gpt4 key购买 nike

我创建了一个简单的计算器。一切都很好;但是,如果我除以零,我想显示一条错误消息。我知道如何做警报弹出窗口,但我不知道如何实现它,所以当我除以零时它会出现。这是我的计算器代码的片段:

- (IBAction)buttonOperationPressed:(id)sender {
if (currentOperation == 0) result = currentNumber;
else {
switch (currentOperation) {
case 1:
result = result + currentNumber;
break;
case 2:
result = result - currentNumber;
break;
case 3:
result = result * currentNumber;
break;
case 4:
result = result / currentNumber;
break;
case 5:
currentOperation = 0;
break;
default:
break;
}
}
currentNumber = 0;
CalcDisplay.text = [NSString stringWithFormat:@"%g",result];
if ([sender tag] == 0) result = 0;
currentOperation = [sender tag];
userInTheMiddleOfEnteringDecimal = NO;

最佳答案

您可以在除法之前添加一个测试,例如改变:

        case 4:
result = result / currentNumber;
break;

到:

        case 4:
if (currentNumber == 0)
// ... do alert here ...
else
result = result / currentNumber;
break;

关于ios - 计算器除以零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16118080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com