在用户做出其他决定之前,我如何让一个流程运行多次?JAVA

在用户做出其他决定之前,我如何让一个流程运行多次?JAVA,java,Java,所以,我做了一个程序,当给定价格(加5%的税)和用户给定的付款时,计算变化。我对本规范的其余要求是: 让程序循环,直到用户决定退出程序 通过输入0或任何负数(即-2) 如果用户输入的付款金额不足以支付价格,请继续要求用户“请输入付款金额”,直到用户输入足够大的付款金额 打印并显示正确的兑换金额以及每种货币的兑换金额。不含税的最高价格是500美元 包含所有可能用户选择的完整输出应如下所示: Cost of transaction (enter 0 or negative to exit; max

所以,我做了一个程序,当给定价格(加5%的税)和用户给定的付款时,计算变化。我对本规范的其余要求是:

  • 让程序循环,直到用户决定退出程序 通过输入
    0
    或任何负数(即
    -2
  • 如果用户输入的付款金额不足以支付价格,请继续要求用户“请输入付款金额”,直到用户输入足够大的付款金额
  • 打印并显示正确的兑换金额以及每种货币的兑换金额。不含税的最高价格是500美元 包含所有可能用户选择的完整输出应如下所示:

    Cost of transaction (enter 0 or negative to exit; max is $500.00): 1000
    Cost of transaction (enter 0 or negative to exit; max is $500.00): 501
    Cost of transaction (enter 0 or negative to exit; max is $500.00): 500
    Amount due (with 5.00% tax): $525.00
    Please enter payment amount: 1000.57
    Change back $475.57
    $100: 4  $50: 1  $20: 1  $10: 0  $5: 1  $1: 0  $0.25: 2  $0.10: 0  $0.05: 1
    $0.01: 2
    Cost of transaction (enter 0 or negative to exit; max is $500.00): 0
    Done.
    
    下面是我的代码:

    /*
    * @author (Zach Daly)
    * <p> (MakeChange.java)
    * <p> (Project2)
    * <p> (Make change)
    */
    
    import java.util.*;
    
    public class MakeChange
    {
        public static void main(String[] args)
        {
            Scanner in = new Scanner(System.in);
            double price;
            double amountPaid;
            int paidInt;
            int priceWithTaxInt;
            double priceWithTaxDbl;
            final double TAX_RATE = .05;
            int change;
            int hundreds, fifties, twenties, tens, fives, ones, quarters, dimes, nickels, pennies;
    
            System.out.print("Cost of transaction (enter 0 or negative to exit; max is $500.00): ");
            price = in.nextDouble();
            priceWithTaxDbl = price + (price * TAX_RATE);
            System.out.printf("Amount due (with 5.00%% tax): $%3.2f\n", priceWithTaxDbl);
            System.out.println("Please enter payment amount: ");
            amountPaid = in.nextDouble();
            paidInt = (int) (amountPaid * 100);
            priceWithTaxInt = (int) (priceWithTaxDbl * 100);
    
            if (price <= 0)
            {
                System.out.println("Done.");
            }
    
            change = paidInt - priceWithTaxInt;
            double changePrint = (double) change / 100;
    
            System.out.println(changePrint);
    
            if (change == 0)
            {
                System.out.println("Exact change! Amazing!");
            }
    
            if (paidInt > priceWithTaxInt)
            {
                hundreds = change / 10000;
                if (hundreds > 0)
                {
                    change %= 10000;
                }
    
                fifties = change / 5000;
                if (fifties > 0)
                {
                    change %= 5000;
                }
    
                twenties = change / 2000;
                if (twenties > 0)
                {
                    change %= 2000;
                }
    
                tens = change / 1000;
                if (tens > 0)
                {
                    change %= 1000;
                }
    
                fives = change / 500;
                if (fives > 0)
                {
                    change %= 500;
                }
    
                ones = change / 100;
                if (ones > 0)
                {
                    change %= 100;
                }
    
                quarters = change / 25;
                if (quarters > 0)
                {
                    change %= 25;
                }
    
                dimes = change / 10;
                if (dimes > 0)
                {
                    change %= 10;
                }
    
                nickels = change / 5;
                if (nickels > 0)
                {
                    change %= 5;
                }
    
                pennies = change;
    
                System.out.printf(
                        "Change back $%.2f\n$100: %d   $50: %d   $20: %d   $10: %d   $5: %d   $1: %d   $0.25: %d   $0.10: %d   $0.05: %d   $0.01: %d\n",
                        changePrint, hundreds, fifties, twenties, tens, fives, ones, quarters, dimes, nickels, pennies);
            }
    
            in.close();
        }
    }
    
    /*
    *@作者(扎克·戴利)
    *(MakeChange.java)
    *(项目2)
    *(进行更改)
    */
    导入java.util.*;
    公共阶级变革
    {
    公共静态void main(字符串[]args)
    {
    扫描仪输入=新扫描仪(系统输入);
    双倍价格;
    支付双倍金额;
    内部paidInt;
    国际贸易价格与税收;
    双倍价格,含TAXDBL;
    最终双重税率=.05;
    智力变化;
    整数一百、五十、二十、十、五、一、四分之一、一角、五分、一分;
    System.out.print(“交易成本(输入0或负以退出;最大值为$500.00):”;
    price=in.nextDouble();
    priceWithTaxDbl=价格+(价格*税率);
    System.out.printf(“到期金额(含5.00%%税费):$%3.2f\n”,价格含TAXDBL);
    System.out.println(“请输入付款金额:”);
    amountPaid=in.nextDouble();
    paidInt=(int)(支付金额*100);
    含税价格=(整数)(含税价格DBL*100);
    if(含税价格)
    {
    百分之一百=变化/10000;
    如果(百分之一百>0)
    {
    变化%=10000;
    }
    50年代=变化/5000;
    如果(五十年代>0)
    {
    变动%=5000;
    }
    二十岁=变化/2000年;
    如果(20岁>0岁)
    {
    变动%=2000;
    }
    十次=变化/1000;
    如果(十位数>0)
    {
    变化%=1000;
    }
    五个=变化/500;
    如果(五个>0)
    {
    变化%=500;
    }
    1=变化/100;
    如果(个数>0)
    {
    变化%=100;
    }
    季度=变动/25;
    如果(四分之一>0)
    {
    变化%=25;
    }
    一角硬币=零钱/10;
    如果(一角硬币>0)
    {
    变化%=10;
    }
    镍币=零钱/5;
    如果(镍币>0)
    {
    变化%=5;
    }
    便士=零钱;
    System.out.printf(
    “更改回$%.2f\n$100:%d$50:%d$20:%d$10:%d$5:%d$1:%d$0.25:%d$0.10:%d$0.05:%d$0.01:%d\n”,
    兑换印钞、百元、五十元、二十元、十元、五元、一元、二角五分、一角、五分、一分钱);
    }
    in.close();
    }
    }
    
    我相信您正在寻找的是do while循环。它将运行一次,并不断重复,直到不满足while条件:

    do {
            System.out.print("Cost of transaction (enter 0 or negative to exit; max is $500.00): ");
            price = in.nextDouble();
            //repeating logic
    
            if (price > 500) {
                price = 0;
                System.out.println("Maximum price was exceeded");
            }
        } while (price >= 0);
    

    我会试试的!非常感谢。现在我唯一的问题是增加了最大用户输入价格为$500.00(不含税)的限制。有什么想法吗?我认为最简单、最快的解决办法是使用一个像-1这样的负片作为退出的哨兵。然后在do循环中添加一个条件以检查价格是否超过500,如果超过500,则将价格重置为0。我将代码段更新为reflect虽然它满足了需求的某些要求,但在其他方面却失败了。它可以运行-很好,但不是最小的。要求用户输入一个数字,进行一些虚拟转换——比如说只需打印出来,然后要求输入下一个数字或终止响应,就足够了。