0

このコードを実行していますが、コンソール出力が間違っています。取得する必要があります:

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;
}

どうしたの?前もって感謝します。

4

1 に答える 1

0

推測ですが、このコードはコマンド ライン ツールですか? もしそうなら、おそらくこれがあなたの問題であることを確認してください。

于 2012-04-14T18:02:47.657 に答える