控制台输出不正确

时间:2012-04-14 17:06:31

标签: objective-c xcode

运行此代码但是控制台输出错误。应该得到:

Converting 100.00 US dollars into foreign currency leaves $900.00
Charging 100.00 in foreign currency leaves $775.00 Converting 100.00 US dollars into foreign currency leaves
$1900.00
Charging 100.00 in foreign currency leaves $1750.00

但我只是得到了:

Converting 100.00 US dollars into foreign currency leaves $900.00

以下是代码:

#import <Foundation/Foundation.h>

@interface Budget:NSObject {
    float exchangeRate;
    double budget;
    double exchangeTransaction;
}

- (void) createBudget:(double)aBudget withExchangeRate:(float) anExchangeRate;

- (void) spendDollars:(double)dollars;

- (void) chargeForeignCurrency:(double)foreignCurrency;

@end

@implementation Budget

- (void) createBudget:(double)aBudget withExchangeRate:(float) anExchangeRate {
    exchangeRate = anExchangeRate;
    budget = aBudget;
}

- (void) spendDollars:(double)dollars {
    budget -= dollars;
    NSLog(@"Converting %.2f US dollars into foreign currency leaves $%.2f", dollars, budget);
}

-(void)chargeForeignCurrency:(double)foreignCurrency {
    exchangeTransaction = foreignCurrency * exchangeRate;
    budget -= exchangeTransaction;
    NSLog(@"Charging %.2f in foreign currency leaves $%.2f", foreignCurrency, budget);
}



@end

int main(int argc, const char * argv[])
{
    double numberDollarsInEuroland = 100;
    double numberEuros = 100;
    double numberDollarsInPoundland = 100;
    double numberPounds = 100;

    Budget *europeBudget = [Budget new];
    [europeBudget createBudget:1000.00 withExchangeRate:1.2500];
    [europeBudget spendDollars:numberDollarsInEuroland];
    [europeBudget chargeForeignCurrency:numberEuros];

    Budget *englandBudget = [Budget new];
    [englandBudget createBudget:2000.00 withExchangeRate:1.5000];
    [englandBudget spendDollars:numberDollarsInPoundland];
    [englandBudget chargeForeignCurrency:numberPounds];



    return 0;
}

怎么了?提前谢谢。

1 个答案:

答案 0 :(得分:0)

只是一个猜测,但这个代码是否在命令行工具中?如果是,请检查this is your problem