joe
joe

Reputation: 29

Change calculator program java (looping)

Write a program that repeatedly reads an integer between 0 and 100 that represents a number of cents. Convert that number of cents to the equivalent number of quarters, dimes, nickels, and pennies. The program should output the maximum number of quarters that will fit, then the maximum number of dimes that will fit into what is left, and so on. Then the program will ask for the next amount. If the amount is negative, the program should quit.

This is what i have so far, im not sure how to make it loop or count the number or each coin.

System.out.println("Enter number of cents (Negative value to quit):");
int cents;
cents = scan.nextInt();

while (cents > 0 )
{
    if (cents >= 25)
    {
        System.out.println("Quarter");
        cents -= 25;
    }
    else if ( cents >= 10 )
    {
        System.out.println("Dime");
        cents -= 10;
    }
    else if (cents >= 5 )
    {
        System.out.println("Nickle");
        cents -= 5 ;
    }
    else if (cents >= 1 )
    {
        System.out.println("Penny");
        cents -= 1;
    }
}

Upvotes: 1

Views: 1100

Answers (2)

Lazar Lazarov
Lazar Lazarov

Reputation: 2532

Well my suggestion is to use a HashMap. Where your code can look something like that:

System.out.println("Enter number of cents (Negative value to quit):");
Map<String, Long> countMap = HashMap<String, Long>();
countMap.put("Quarter", 0);
countMap.put("Dime", 0);
countMap.put("Nickle", 0);
countMap.put("Penny", 0);

   int cents;
   cents = scan.nextInt();

   while (cents > 0 )
   {
       if (cents >= 25)
       {
           System.out.println("Quarter");
           countMap.put("Quarter", countMap.get("Quarter")+1L);
           cents -= 25;
        }
        else if ( cents >= 10 )
        {
            System.out.println("Dime");
            countMap.put("Dime", countMap.get("Dime")+1L);
            cents -= 10;
        }
        else if (cents >= 5 )
        {
            System.out.println("Nickle");
            countMap.put("Nickle", countMap.get("Nickle")+1L);
            cents -= 5 ;
        }
        else if (cents >= 1 )
        {
            System.out.println("Penny");
            countMap.put("Penny", countMap.get("Penny")+1L);
            cents -= 1;
        }
}

Now you have all you need.

System.out.println("Quarter: " + countMap.get("Penny"));
System.out.println("Dime: " + countMap.get("Dime"));
System.out.println("Nickle: " + countMap.get("Nickle"));
System.out.println("Penny: " + countMap.get("Penny"));

Upvotes: 0

Arnaud Denoyelle
Arnaud Denoyelle

Reputation: 31225

You can decompose the problem in 2 parts :

  • do ask an amount while the amount is non negative
  • Decompose the given amount with integer division

1) Ask an input in the main method

Scanner scan = new Scanner(System.in);
int input;
do {
  input = scan.nextInt();
  decompose(input);
} while (input > 0);

2) Write the decomposition in another method :

public static void decompose(int cents) {
  if(cents >= 25) {
    int quot = cents / 25;
    System.out.println(quot + " Quarter");
    cents -= quot * 25;
  }

  if(cents >= 10) {
    int quot = cents / 10;
    System.out.println(quot + " Dime");
    cents -= quot * 10;
  }

  [...]
}

Upvotes: 0

Related Questions