I have a problem in my code and i can't find the answer for it. I only can use if and else and can't use other classes for an example Math.
The code save a value and try to divide in euro coins. If i enter 4,31 the result is 2x2e + 1x20c + 1x1c and this is ok but if i enter the value 1,20 the result is 1e + 1x10c + 1x5c + 2x2c + 1x1c but the right result is 1e + 1x20c. I had to add 0.001 in the 1cent coin because if i don't i'll not get a print for it. Adding this is wrong too.
If somebody could help me i would be very grateful. Regards.
Code:
import java.util.Scanner;
public class Coins {
public static void main(String[] args){
Scanner in= new Scanner(System.in);
int e2 = 0, e1 = 0,c50 = 0, c20=0,c10 = 0,c5 = 0,c2 = 0,c1;
double v;
System.out.println("Quantia em euros: ");
v = in.nextDouble();
e2 = (int)v/2;
v=v-e2*2;
e1=(int)v;
v=(v-e1)*100;
c50=(int)v/50;
v=v-c50*50;
c20=(int)v/20;
v=v-c20*20;
c10=(int)v/10;
v=v-c10*10;
c5=(int)v/5;
v=v-c5*5;
c2=(int)v/2;
v=v-c2*2;
c1=(int)(v+0.001);
if(e2!=0)System.out.print(e2+"X2Eur ");
if(e2!=0&&!(e1==0&&c50==0&&c20==0&&c10==0&&c5==0&&c2==0&&c1==0))System.out.print("+ ");
if(e1!=0)System.out.print(e1+"X1Eur ");
if(e1!=0&&!(c50==0&&c20==0&&c10==0&&c5==0&&c2==0&&c1==0))System.out.print("+ ");
if(c50!=0)System.out.print(c50+"X50c ");
if(c50!=0&&!(c20==0&&c10==0&&c5==0&&c2==0&&c1==0))System.out.print("+ ");
if(c20!=0)System.out.print(c20+"X20c ");
if(c20!=0&&!(c10==0&&c5==0&&c2==0&&c1==0))System.out.print("+ ");
if(c10!=0)System.out.print(c10+"X10c ");
if(c10!=0&&!((c5==0&&c2==0&&c1==0)))System.out.print("+ ");
if(c5!=0)System.out.print(c5+"X5c ");
if(c5!=0&&!(c2==0&&c1==0))System.out.print("+ ");
if(c2!=0)System.out.print(c2+"X2c ");
if(c2!=0&&!(c1==0))System.out.print("+ ");
if(c1!=0)System.out.print(c1+"X1c");
}
}