Search code examples
javaeclipsewhile-loopdo-whilenegative-number

Why is my while going in a infinity loop? How do I fix it?


I dont know why my do while is going on infinity and not showing my output which should be how many quaters, dimes ,etc has that amount of money that the user input in the system.

Scanner scan = new Scanner(System.in);

    System.out.println("Enter the amount of money: ");
    float dinero= scan.nextFloat();
    int quaters=0;
    int dimes =0;
    int nickle =0;
    int pennys =0;
    boolean b=false;
    do{
        if(dinero>=0.25){

            dinero=(float) (dinero-0.25);
            quaters++;

        }else if(dinero<0.25 && dinero>=0.10){

            dinero=(float) (dinero-0.10);
            dimes++;

        }else if(dinero<0.10 && dinero>=0.05){

            dinero=(float) (dinero-0.05);
            nickle++;

        }else if(dinero<0.05 && dinero<0){

            dinero=(float) (dinero-0.01);
            pennys++;
        }else{
            b=true;
        }


    }while(b==false);

    System.out.println("Quater :"+quaters);
    System.out.println("Dimes :"+dimes);
    System.out.println("Nickle :"+nickle);
    System.out.println("Pennys :"+pennys);

Please help i know this is a dumb question but help will be much apprishiated.


Solution

  • I fix it it was that my last condition was wrong. Also the while was wrong

    else if(dinero<0.05 && dinero>=0.01){
    
    // was the the fix for the last else if         
    
    while(dinero>0.01);
     // and here is the while