Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
我不太清楚这个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;
}

最佳答案

while循环具有条件。只要条件为真,它将执行身体。在您的情况下,条件不会更改,因为比较的两个变量都没有在循环内更改其值。您应该增加总数或减少目标。

08-06 01:08
查看更多