如何通过提示退出程序,如何运行循环

How can I exit a program through prompt, how can I run a loop

嗨,我是编程新手,我被分配 "Minimum Coins Program" 参加 class 我正在参加的课程。我已经完成了它的主要代码并且运行良好。但部分参数是,如果用户输入零,程序将退出,否则程序将继续循环。我已尝试查找答案,但 none 到目前为止有效。

这是我所拥有的,但我似乎无法掌握循环。这是我们的第一个非流程图作业。此外,如果您对改进我已有的东西有任何建议,我们也将不胜感激(这位教授是一个非常苛刻的评分者)。

如何通过用户输入零让程序退出,以及如何让程序循环直到用户输入零。到目前为止,该程序只运行一次,当我输入零时,它会列出最小更改量

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

你的程序(通常)在 1 个循环后结束,因为你的代码随着它的进行减少 amount,如果所需的硬币数为零,循环结束,因为 amount 减少为零.

试试这个:

while (true) {
    // print and read amount
    if (amount == 0)
        break;
    // rest of code
}