gpt4 book ai didi

使用循环和 if/else 的现金找零程序

转载 作者:行者123 更新时间:2023-11-30 17:38:48 24 4
gpt4 key购买 nike

编写一个程序,允许用户输入最多 200.00 美元的现金金额,然后计算并打印以下面额(20、10、5、1、.25、.10、.05、.01)的金额。我想我已经弄清楚了如何获得面额(除法/模数)的基础知识,但 do/while 和 if/else 的结构给我带来了麻烦。我不断收到一个错误,即我需要一个 while 语句,即使我已经输入了它及其条件(如下所示),但我也对在哪里放置范围提示感到有点困惑(如果用户输入了一些内容)负数或高于 200)。任何建议/指导将不胜感激!

double amt_ent;
int twenty, ten, five, one, quarter, dime, nickel, penny, remainder;

printf ("Enter a dollar amount up to $200.00:");
scanf ("%lf", &amt_ent);

do
{ printf ("Name - Assignment 2 - Change-O-Matic\n");
printf ("Amount entered: $%.2lf\n", ((amt_ent*100)/100));
printf ("Change breakdown:\n");

{ /*Change in twenties*/
twenty= (int) amt_ent/20;
if (twenty >= 2)
printf("%i\t$20.00s\n", twenty);
if (twenty == 1)
printf ("%i\t$20.00\n", twenty);
else

/*Change in tens*/
remainder = twenty % 20;
ten = remainder/10;

if (ten >=2)
printf ("%i\t$10.00s\n", ten);
if (ten == 1)
printf ("%i\t$10.00\n", ten);
else

/*Change in fives*/
remainder = ten % 10;
five = remainder/10;

if (five >= 2)
printf ("%i\t$5.00s\n", five);
if (five == 1)
printf ("%i\t$5.00\n", five);
else

/*Change in ones*/
remainder = five % 5;
one = remainder/1;

if (one >= 2)
printf ("%i\t$1.00s\n", one);
if (one == 1)
printf ("%i\t$1.00\n", one);
else

/*Change in quarters*/
remainder = one % 1;
quarter = remainder/.25;

if (quarter >= 2)
printf ("%i\t$.25s\n", quarter);
if (quarter == 1)
printf ("%i\t$.25\n", quarter);
else

/*Change in dimes*/
remainder = quarter % 4;
dime = remainder/.10;

if (dime >= 2)
printf ("%i\t$.10s\n", dime);
if (dime == 1)
printf ("%i\t$.10\n", dime);
else

/*Change in nickels*/
remainder = dime % 10;
nickel = remainder/.05;

if (nickel >= 2)
printf ("%i\t$.05s\n", nickel);
if (nickel == 1)
printf ("%i\t$.05\n", nickel);
else

/*Change in pennies*/
remainder = nickel % 20;
penny = remainder/100;

if (penny >= 2)
printf ("%i\t$.01s\n", penny);
if (penny == 1)
printf ("%i\t$.01\n", penny);
}

while ((amt_ent <= 200.00) && (amt_ent >= 00.00));}

return 0;

最佳答案

关于您的错误, printf ("Change breakdown:\n"); 之后有额外的大括号。并且您不需要在 while 语句之后放置右大括号。 while ((amt_ent <= 200.00) && (amt_ent >= 00.00));}也删除它。

对于您的有效金额处理问题,有一个 continue 命令跳过剩余的循环并在遇到时从头开始重复。您可以使用它。

do
{
printf ("Enter a dollar amount up to $200.00:");
scanf ("%lf", &amt_ent);
if(amount_ent < 00.00 || amount_ent>200.00)
continue;
printf ("Name - Assignment 2 - Change-O-Matic\n");
printf ("Amount entered: $%.2lf\n", ((amt_ent*100)/100));
printf ("Change breakdown:\n");

如果输入的金额无效,则 continue语句将执行并保持循环被跳过。然后printf ("Enter a dollar amount up to $200.00:");将被执行。因此您可以看到,除非输入正确的金额值,否则用户将无法继续继续。

关于使用循环和 if/else 的现金找零程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22032680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com