質問

For school I've been given an exercise where I have to calculate the change to give to someone based on the amount they have to pay and the amount they have paid. However, I'm having a small issue since I'm not able to enter the for that is inside the it that validates whether it's possible or not to give change.

What am I doing wrong?

on (release){
//Defines variables
var nDespesa:Number = parseInt(caixaDespesa.text);
var nPago:Number = parseInt(caixaPago.text);
var nTroco:Number = nPago - nDespesa;

//Defines currency
var aDinheiro:Array = [500, 200, 100, 50, 20, 10, 5, 2, 1, 0.50, 0.20, 0.10, 0.05, 0.02, 0.01];

//Defines the number of each note/coin to receive
var aReceber:Array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

//Verifies if the amount paid is bigger than the amount in debt
if(nPago > nDespesa){
    //Shows the change on the screen
    caixaMsg.text = "O troco será de " + nTroco + " euros";

    //Calculates the number of times each note/coin sould be given by matching the array positions
    for(var i:Number=0; i<aDinheiro.lenght; i++)
    {
        trace("Entrou no for");
        while(nTroco > aDinheiro[i]){
            trace("Entrou no while");
            aReceber[i]++;
            trace(aReceber[i]);
            nTroco -= aDinheiro[i];
            trace(nTroco);
        }
    }
}
else{
    //Shows on the screen that it's not possible to calculate the change because the debt is bigger than the amount paid
    caixaMsg.text = "O valor pago não corresponde ao valor da dívida";
}

//Shows the number of times each note/coin sould be given on the corresponding box 
caixa1c.text = aReceber[14];
caixa2c.text = aReceber[13];
caixa5c.text = aReceber[12];
caixa10c.text = aReceber[11];
caixa20c.text = aReceber[10];
caixa50c.text = aReceber[9];
caixa1e.text = aReceber[8];
caixa2e.text = aReceber[7];
caixa5e.text = aReceber[6];
caixa10e.text = aReceber[5];
caixa20e.text = aReceber[4];
caixa50e.text = aReceber[3];
caixa100e.text = aReceber[2];
caixa200e.text = aReceber[1];
caixa500e.text = aReceber[0];

}

役に立ちましたか?

解決

Is it just because you mispelt length?

for(var i:Number=0; i<aDinheiro.lenght; i++)

should be;

for(var i:Number=0; i<aDinheiro.length; i++)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top