-1

任何人都可以帮助解释为什么如果遵循 if 我不能拥有 else 吗?错误是说预期的表达,但我不知道这意味着什么

// 将变量声明为整数 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);
4

3 回答 3

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]);
}
于 2013-02-11T04:08:39.587 回答
1

您在 . 块周围缺少花括号if。当一个块中有多个语句时,必须在所有语句周围加上花括号。与 Python 等其他一些编程语言不同,Objective C 中的缩进是微不足道的。相关块中的所有语句分组都由花括号完成。

于 2013-02-11T04:06:43.473 回答
0

在一个ifelse之间应该只有一个语句。

你的**one statement可以是

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

{
    a = b;
    b = 20;
}
于 2013-02-11T04:25:40.847 回答