简体   繁体   中英

My loop is wrong. Brand new to Java. Any insight as to where I am going wrong? I am supposed to calculate coins

I am working on a project that calculates the number of coins used to produce a certain total of change. For some reason, my output is only reading this: Quarters: Dimes: end.

It doesn't print the integer being calculated, and does not continue past dimes. I have tried using different loops and print statements, but am at a loss. Any insight of hints would be greatly appreciated.

/*
 * To change this license header, choose License Headers in Project     Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package minimumcoinsproject;
import java.util.Scanner;
 /**
 *
  * @author user
  */
 public class MinimumCoinsProject {

/**
 *
 * @author 
 */
static public class MinumimCoinsProject//a class is not an object, but a     blueprint for an object. It is the building blocks.
{

}
public static void main(String[] args) {
    // TODO code application logic here
    { //initialization

        int change = 0;
        int quarters = 0;
        int dimes = 0;
        int nickels = 0;

    Scanner in;
    in = new Scanner(System.in);
    System.out.println("Lets calclulate the minimum coins.\n");
    System.out.println("PLease enter amount of change.(1-99)\n");
    change=in.nextInt(); //asks for the amount of change to calculate


    //start of while loop
        while  ( change >= 25)
    {
        quarters = quarters+1;
        change = quarters - 25;
        System.out.printf("Quarters:\n", quarters);
    } 
        if (10 >= change)
    {

        dimes = dimes + 1;
        change = change - 10;
        System.out.printf("Dimes: \n", dimes);

    }
        if  (change >=5) 
    {

        nickels = nickels + 1;            
        change = change - 5;
        System.out.printf("Nickles: \n",nickels);

    }
        if ( change == 0)
        System.out.printf("Pennies: \n", change);
    }
}

}

First while loop :

change = quarters - 25;

should be

change = change - 25;

Then the three if loops should also be while loops.

Change:

System.out.printf("Quarters:\n", quarters);

To this:

System.out.printf("Quarters:%d\n", quarters);

Do the same for dimes, nickels, pennies. I looked further and found some more issues with your code. This should get it working:

while (change >= 25) {
            quarters = quarters + 1;
            change = change - 25;
        }
        if (quarters > 0) {
            System.out.printf("Quarters:%d\n", quarters);
        }

        if (change >= 10) {

            dimes = dimes + 1;
            change = change - 10;
            System.out.printf("Dimes:%d\n", dimes);

        }
        if (change >= 5) {

            nickels = nickels + 1;
            change = change - 5;
            System.out.printf("Nickles:%d\n", nickels);

        }
        if (change > 0) {
            System.out.printf("Pennies:%d\n", change);
        }

I think this should work:

    int change = 0;
    int quarters = 0;
    int dimes = 0;
    int nickels = 0;

    Scanner in;
    in = new Scanner(System.in);
    System.out.println("Lets calclulate the minimum coins.\n");
    System.out.println("PLease enter amount of change.(1-99)\n");
    change=in.nextInt(); //asks for the amount of change to calculate


    //start of while loop
    while  ( change >= 25)
    {
        quarters = quarters+1;
        change = change - 25;

    } 
    System.out.printf("Quarters:\n"+quarters+"\n");
    while (change>=10)
    {

        dimes = dimes + 1;
        change = change - 10;


    }
    System.out.printf("Dimes: \n"+dimes+"\n");
    while(change>=5)
    {

        nickels = nickels + 1;            
        change = change - 5;


    }
    System.out.printf("Nickles: \n"+nickels+"\n");
    if ( change>0)
        System.out.printf("Pennies: \n"+change+"\n");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM