-2

Hi I am new to programming and have been assigned "Minimum Coins Program" for a class I am taking. I have finished the main code for it and it runs fine. But part of the parameters is that if the user enters a zero the program will exit, if not the program will continue looping. I have tried looking up answers but none have worked so far.

Here is what I have, but I can't seem to grasp looping. This is our first non flowchart assignment. Also if you have any suggestions on improving what I already have that would also be appreciated(this professor is a very harsh grader).

How can I get the program to exit through the user entering zero, and how can I keep the programming looping until the user enters zero. As of now the program just runs once and when I enter zero it lists the minimum amount of change

package mincoins;

import java.util.Scanner;

public class MinCoins
{

public static void main(String[] args)
{ //start code

    //initialization
    Scanner input = new Scanner(System.in); //create input class to get change data 
    int amount, quartercount = 0, dimecount = 0, nickelcount = 0, penniecount = 0; 
    amount = 1;


    while (amount != 0)
    {

        System.out.println("Please Enter amount of change (1-99) or ZERO to EXIT");
        System.out.println("");

        amount = input.nextInt();

        {

            while (amount > 25)
            {
                amount = amount - 25;
                quartercount++;
            }

            while (amount > 10)
            {
                amount = amount - 10;
                dimecount++;
            }

            while (amount > 5)

            {
                amount = amount - 5;
                nickelcount++;
            }
            System.out.println("");

            System.out.println("Quarters: " + quartercount);

            System.out.println("Dimes: " + dimecount);

            System.out.println("Nickles: " + nickelcount);

            System.out.println("Pennies: " + amount);

            System.out.println("");

        }
    }


}//main

}//class
S.R.
  • 3
  • 2
  • 2
    Okay, since it's your first time posting a question, first I'd ask can you define a question which you want to be answered? what exactly is the issue you're facing? next, can you also have a read at the documentation. "How do I ask a good question?" --> https://stackoverflow.com/help/how-to-ask – Ousmane D. Apr 11 '17 at 02:15
  • Hi I added more information, I hope this is easier to understand. I want to know how I can have the program exit after a user enters the prompt, and how to keep the program looping if the user does not enter the exit prompt. – S.R. Apr 11 '17 at 02:41
  • `if (amount == 0) return;` – Bohemian Apr 11 '17 at 04:24
  • Welcome to Stack Overflow! Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. –  Apr 11 '17 at 04:45
  • Welcome to Stack Overflow! Please read [What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask) before attempting to ask more questions. –  Apr 11 '17 at 04:45
  • Welcome to Stack Overflow! [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Apr 11 '17 at 04:45

1 Answers1

0

Your program (often) ends after 1 loop because your code reduces amount as it progresses, and if the number of pennies required is zero, the loop ends because amount is reduced to zero.

Try this:

while (true) {
    // print and read amount
    if (amount == 0)
        break;
    // rest of code
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722