Objective-C代码说明

时间:2013-02-11 04:05:22

标签: objective-c

任何人都可以帮助解释为什么如果关注if,我就不能拥有其他内容?错误是说预期的表达,但我不知道它是什么意思

//将变量声明为整数         int account,depAmount,withAmount;

    Account *Account1=[[Account alloc] init];

    // Prompt user to input account number
    NSLog(@"Please input your account number here: ");
    scanf("%i", &account); // Accept user input

    if (account==1000)
        NSLog(@"How much do you want to deposit: ");
        scanf("%i", &depAmount);

        [Account1 setaccountNumber: account];
        [Account1 setbegBalance: 900.50];
        [Account1 addDeposit: depAmount];

        NSLog(@"How much do you want to withdrawl: ");
        scanf("%i", &withAmount);
        [Account1 setnewbegBalance: 900.50 + depAmount];
        [Account1 subtractWithdrawal: withAmount];

    NSLog(@"Your account number is: %i\nBeginning balance: %f\nNew balance is: %f", [Account1 accountNumber], [Account1 begBalance], [Account1 newBalance]);

    else if (account==2000)
        NSLog(@"How much do you want to deposit: ");
        scanf("%i", &depAmount);

        [Account1 setaccountNumber: account];
        [Account1 setbegBalance: 700.75];
        [Account1 addDeposit: depAmount];

        NSLog(@"Your account number is: %i\nBeginning balance: %f\nNew balance is: %f", [Account1 accountNumber], [Account1 begBalance], [Account1 newBalance]);

    else if (account==3000)
        NSLog(@"How much do you want to deposit: ");
        scanf("%i", &depAmount);

3 个答案:

答案 0 :(得分:2)

你需要花括号作为你想要作为if条件的一部分执行的指令。如果没有花括号,则只执行if语句后面的1语句。所以添加像这样的大括号:

if (account==1000)
{
    NSLog(@"How much do you want to deposit: ");
    scanf("%i", &depAmount);

    [Account1 setaccountNumber: account];
    [Account1 setbegBalance: 900.50];
    [Account1 addDeposit: depAmount];

    NSLog(@"How much do you want to withdrawl: ");
    scanf("%i", &withAmount);
    [Account1 setnewbegBalance: 900.50 + depAmount];
    [Account1 subtractWithdrawal: withAmount];

NSLog(@"Your account number is: %i\nBeginning balance: %f\nNew balance is: %f", [Account1 accountNumber], [Account1 begBalance], [Account1 newBalance]);
}

答案 1 :(得分:1)

你在if区块周围缺少大括号。如果块中有多个语句,则必须在所有语句周围添加大括号。与其他一些编程语言(如Python)不同,Objective C中的缩进是无关紧要的。相关块中的所有语句分组都是通过大括号完成的。

答案 2 :(得分:0)

ifelse之间,应该只有一个声明。

您的** one statement可以

A。)单一陈述,例如a = b;
B.)在一个块中分组的多个语句。

{
    a = b;
    b = 20;
}