For a university job I have to make a distributor. Now in one part I have to calculate how many coins I will return to give the change.
For example I have a water drink that costs € 2.20
. The person pays with 3
so their change would be 0.80
. The program does that without any problem.
When the person takes the order, they enter (through a listbox
containing the values of each currency) how many coins of each value they receive.
And now I have to calculate and show how many coins and value when I'm going to return the change.
I'm trying it this way:
- I made 3 arrays
1 two-dimensional array to store the value of the coin and the amount of coins of each value.
And a simple decimal array with possible values of the change
int posiblesValores []
(Read here bedragen . It's a program in Dutch)
posiblesValores= new decimal [19];
and to each one I gave these values:
bedragen[0]= 1.90m ;
bedragen[1]= 1.80m ;
bedragen[2]= 1.70m ;
bedragen[3]= 1.60m ;
bedragen[4]= 1.50m ;
bedragen[5]= 1.40m ;
bedragen[6]= 1.30m ;
bedragen[7]= 1.20m ;
bedragen[8]= 1.10m ;
bedragen[9]= 1.00m ;
bedragen[10]= 0.90m ;
bedragen[11]= 0.80m ;
bedragen[12]= 0.70m ;
bedragen[13]= 0.60m ;
bedragen[14]= 0.50m ;
bedragen[15]= 0.40m ;
bedragen[16]= 0.30m ;
bedragen[17]= 0.20m ;
bedragen[18]= 0.10m ;
Array bidimensional = decimal cambio[,]= new decimal [5,2]
(Change in Dutch. teruggave )
teruggave[0, 0] = 2.00m;
teruggave[1, 0] = 1.00m;
teruggave[2, 0] = 0.50m;
teruggave[3, 0] = 0.20m;
teruggave[4, 0] = 0.10m;
Then I made a method with if
:
wisselgeld
(value to be returned)
With this I try that for example if the value to return is equal to 1.90
then show me that I have to return a coin of 1
, one of 0.50
and two of 0.20
.
The problem is when I run the program for each value that I have to return it gives me the same screen whenever it is the first change of if
bone as if value to return always out 1.90
.
Already verify and the program takes well the value of the change and the value of array
of possible values and compares it well. For example, if 0.30
calculates:
wisselgeld = 0.30
bedragen[0] = 1.90
if (wisselgeld == bedragen[0]) //NO SE CUMPLE LA CONDICIÓN
{
teruggave[1, 1] = 1;
teruggave[2, 1] = 1;
teruggave[3, 1] = 2;
}
(in teruggave[0-5,1]
I keep the amounts of coins)
My question is because if you compare well and for example it says 0.30
is not equal to 1.90
the program of the value to array teruggave [ ,1]
as if it were equal. It is the same with any exchange value. Always assign values to array
as if it were equal to 1.90
.
Could it be that there is another way to do this and I am doing it very complicated? Why does not the if
work for me here?
This is the method with if
:
void TerugGaveBedrag()
{
if (wisselgeld == bedragen [0])
{
teruggave[1, 1] = 1;
teruggave[2, 1] = 1;
teruggave[3, 1] = 2;
}
else if (wisselgeld == bedragen [1])
{
teruggave[1, 1] = 1;
teruggave[2, 1] = 1;
teruggave[3, 1] = 1;
teruggave[4, 1] = 1;
}
else if (wisselgeld == bedragen[2])
{
teruggave[1, 1] = 1;
teruggave[2, 1] = 1;
teruggave[3, 1] = 1;
}
else if (wisselgeld == bedragen[3])
{
teruggave[1, 1] = 1;
teruggave[2, 1] = 1;
teruggave[4, 1] = 1;
}
else if (wisselgeld == bedragen[4])
{
teruggave[1, 1] = 1;
teruggave[2, 1] = 1;
}
else if (wisselgeld == bedragen[5])
{
teruggave[1, 1] = 1;
teruggave[3, 1] = 2;
}
else if (wisselgeld == bedragen[6])
{
teruggave[1, 1] = 1;
teruggave[3, 1] = 1;
teruggave[4, 1] = 1;
}
else if (wisselgeld == bedragen[7])
{
teruggave[1, 1] = 1;
teruggave[3, 1] = 1;
}
else if (wisselgeld == bedragen[8])
{
teruggave[1, 1] = 1;
teruggave[4, 1] = 1;
}
else if (wisselgeld == bedragen[9])
{
teruggave[1, 1] = 1;
}
else if (wisselgeld == bedragen[10])
{
teruggave[2, 1] = 1;
teruggave[3, 1] = 2;
}
else if (wisselgeld == bedragen[11])
{
teruggave[2, 1] = 1;
teruggave[3, 1] = 1;
teruggave[4, 1] = 1;
}
else if (wisselgeld == bedragen[12])
{
teruggave[2, 1] = 1;
teruggave[3, 1] = 1;
}
else if (wisselgeld == bedragen[13])
{
teruggave[2, 1] = 1;
teruggave[4, 1] = 1;
}
else if (wisselgeld == bedragen[14])
{
teruggave[2, 1] = 1;
}
else if (wisselgeld == bedragen[15])
{
teruggave[3, 1] = 2;
}
else if (wisselgeld == bedragen[16])
{
teruggave[3, 1] = 1;
teruggave[4, 1] = 1;
}
else if (wisselgeld == bedragen[17])
{
teruggave[3, 1] = 1;
}
else
{
teruggave[4,1] = 1;
}
}