Objective-C 代码错误 [英] objective-c code error

查看:31
本文介绍了Objective-C 代码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#import <Foundation/Foundation.h>
#import "Calculator.h"
#import "ctype.h"
int main(int argc, const char * argv[])
{
    double value1,value2;
    char operator;
    Calculator *myCal =[[Calculator alloc]init ];

    NSLog(@"Type in your expression");
    scanf("%1f %c %1f",&value1,&operator,&value2);

    [myCal setAccumulator:value1];
    if (operator =='+')
        [myCal add:value2];

    else if (operator == '-')
         [myCal subtract:value2];

     else if (operator == '*')
         [myCal multiply:value2];

     else if (operator == '/')
             [myCal divide:value2];

     NSLog(@"%.2f",[myCal accumulator]);

    }
    return 0;
}

我找不到这段代码的任何错误,我输入了书中的代码,代码与书中的代码相同.但是 xcode 告诉我他们有两个错误,第一个是预期标识符"或("第二个是预期的外部声明"

I cant find out any error about this code, I type code from the book, the code is same as on book. but the xcode tell me they have two errors, first one is 'Expected identifier or '(' second one is 'Expected external declaration'

推荐答案

return 语句前有一个伪大括号:

You have a spurious brace before the return statement:

}  <--- remove this
return 0;

EDIT:改用这个 sscanf() 语句,以便您可以输入正确的浮点数:

EDIT: Use this sscanf() statement instead so you can enter proper floating point numbers:

scanf("%f %c %f", &value1, &operator, &value2);

这篇关于Objective-C 代码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆