为什么我的 while 循环允许我不断输入值,但循环什么都不做

huangapple go评论51阅读模式
英文:

Why does my while loop let me continuously enter values, but the loop does nothing

问题

以下是您代码的翻译部分:

#include <iostream>

int main() {
    int money, coupons, chocolate, ogCoupons, newChocolate, couponsUsed;

    std::cout << "你想买多少块巧克力? " << std::endl;
    std::cin >> money;

    coupons = money;
    chocolate = money;
    ogCoupons = coupons;

    std::cout << "\n你获得了 " << coupons << " 张优惠券。" << std::endl;

    while (coupons >= 6) {
        coupons -= 6;
        couponsUsed = (ogCoupons - coupons); //我认为问题出在这里
        chocolate++;
        newChocolate = couponsUsed;
        coupons += newChocolate;
        ogCoupons = coupons;
    }

    std::cout << "\n你还剩下 " << coupons << " 张优惠券和 " << chocolate << " 块巧克力。" << std::endl;

    return 0;
}

请注意,这段代码的目标是模拟购买巧克力和使用优惠券的过程。如果您有任何问题或需要进一步的解释,请随时提出。

英文:
#include &lt;iosteam&gt;

int main(){
int money, coupons, chocolate, ogCoupons, newChocolate, couponsUsed;

std :: cout &lt;&lt; &quot;How many chocolate bars do you want to buy? &quot; &lt;&lt; std :: endl;
std :: cin &gt;&gt; money;

coupons = money;
chocolate = money;
ogCoupons = coupons;

std :: cout &lt;&lt; &quot;\nYou got &quot; &lt;&lt; coupons &lt;&lt; &quot; coupons.&quot; &lt;&lt; std :: endl;


while(coupons &gt;= 6)
    {
        coupons -= 6;
        couponsUsed = (ogCoupons - coupons); //I think problem lies here
        chocolate++;
        newChocolate = couponsUsed;
        coupons += newChocolate;
        ogCoupons = coupons;
    } 

std :: cout &lt;&lt; &quot;\nYou have &quot; &lt;&lt; coupons &lt;&lt; &quot; coupons left and &quot; &lt;&lt;  chocolate &lt;&lt; &quot; chocolate bars.&quot; &lt;&lt; std :: endl;

return 0;
}

Code it supposed to do this: If you have 22 dollars, you can initially buy 22 chocolate bars.  This will give you 22 coupons.  You can redeem 18 of them for 3 additional chocolate bars, for a total now of 25.  These 3 additional bars gives you 3 additional coupons, for a total of 7 now.  This gives you enough coupons to buy one more chocolate bar, with one more coupon, for a total now of 26 chocolate bars and 2 left over coupons.

The problem lies withing the while loop. When I try to subtract the two variables in couponsUsed the loop lets me input random integers and character, but nothing happens.

答案1

得分: 4

以下是您要翻译的内容:

Your code is confusing. Maybe think of it this way:

while (coupons >= 6) {
     // So far, so good. If we're in this loop, we have enough
     // coupons to trade for a chocolate bar. So buy exactly one:
     ++chocolate;
     coupons -= 6;

     // And because we bought a chocolate bar, we get one more coupon:
     ++coupons;
}

I didn't test it, but that's a lot easier to read and make sense of. Give it a try.

A common error in programming is to make the problem too complicated. If things stop making sense, then back away and ask if you're keeping it simple.

Also, it's better to define variables when you're about to use them. You define a bunch of variables at the top of your function that you ultimately don't really need, or if you do, only within the loop. So declare them where you use them. I'd write the top more like this:

int money;

std :: cout << "How many chocolate bars do you want to buy? " << std :: endl;
std :: cin >> money;

// Chocolate bars are $1, so we get 1 bar per dollar
// and 1 coupon for each bar we buy.
int coupons = money;
int chocolate = money;

And you flat out don't need the other variables.
英文:

Your code is confusing. Maybe think of it this way:

while (coupons &gt;= 6) {
     // So far, so good. If we&#39;re in this loop, we have enough
     // coupons to trade for a chocolate bar. So buy exactly one:
     ++chocolate;
     coupons -= 6;

     // And because we bought a chocolate bar, we get one more coupon:
     ++coupons;
}

I didn't test it, but that's a lot easier to read and make sense of. Give it a try.

A common error in programming is to make the problem too complicated. If things stop making sense, then back away and ask if you're keeping it simple.

Also, it's better to define variables when you're about to use them. You define a bunch of variables at the top of your function that you ultimately don't really need, or if you do, only within the loop. So declare them where you use them. I'd write the top more like this:

int money;

std :: cout &lt;&lt; &quot;How many chocolate bars do you want to buy? &quot; &lt;&lt; std :: endl;
std :: cin &gt;&gt; money;

// Chocolate bars are $1, so we get 1 bar per dollar
// and 1 coupon for each bar we buy.
int coupons = money;
int chocolate = money;

And you flat out don't need the other variables.

huangapple
  • 本文由 发表于 2023年2月10日 04:56:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定