我不能完全弄清楚这个while循环。我了解这个概念,我只是不知道该怎么增加它。另外,由于某种原因,我的跑步总数不起作用?while循环不起作用,如果没有更改终止它
这背后的想法是设置一个钱储存在一个罐子的目标。每次我在罐子里放入一定数量的金钱时,我都希望它能给我罐子里的所有金钱,并告诉我需要多少钱才能放入罐子才能达到我的目标。
这里是我的代码:
#include <stdio.h>
int main() {
int goal;
int total = 0;
int deposite;
int ammountNeeded;
printf("How much money would you like to save?\n ");
scanf("%i", &goal);
printf("How much money are you putting in the jar?\n");
scanf("i%", &deposite);
total = total + deposite;
ammountNeeded = goal - deposite;
while (goal > total) {
printf("How much money are you putting in the jar?\n ");
scanf("i%", &deposite);
printf("You have saved R%i. ", total);
printf("You need to save another R%i in order to reach your goal.\n ",ammountNeeded);
}
if (total >= goal) {
printf("Well done! you have sucsessfully saved R%i", goal);
}
return 0;
}
此代码既不是C#,目标C,或C++。只应用实际有效的标签。 –
您需要将'deposite'添加到'total'并将其存回'total',否则值永远不会改变,循环也不会退出,正如您所观察的那样。 – aglasser
我看到几个拼写错误。我没有看到'while'循环内'total'或'ammountNeeded' [sic]的调整。切勿使用'scanf'。 – zwol