整数仍然随机出现?基本编码错误。 C

时间:2015-02-03 04:09:08

标签: c

对于家庭作业,我们必须提出一个程序,该程序可以查看一个人拥有多少游戏积分,两个不同的游戏耗费多少游戏,并计算出您可以使用所拥有的积分购买多少补偿。我认为该程序只需选择成本较低的通电,并将总数除以该数量。

我认为数学是对的?虽然最后一个printf中的整数总是随机出现,好像他们根本没有阅读任何内容。我可能看不到一个明显的问题,因为我还不知道。

#include <stdio.h>

int main()
{
    //listen for cost of first powerup
    int powerone;
    printf("How much does the first powerup cost (in credits)?\n");
    scanf("%d", &powerone);

    //listen for cost of second powerup
    int powertwo;
    printf("How much does the second powerup cost (in credits)?\n");
    scanf("%d", &powertwo);

    //listen number of credits
    int credits;
    printf("How many credits do you currently have?\n");
    scanf("%d", &credits);

    //figure out how many powerups you can get
    int answer;
    {
        if (powerone > powertwo) {
            int answer = credits / powertwo;
        }
        else if (powerone < powertwo) {
            int answer = credits / powerone;
        }
        else if (powerone == powertwo) {
            int answer = credits / powerone;
        }
    }

    int LEFTOVER;
    {
        if (powerone > powertwo) {
            LEFTOVER = credits - (answer * powertwo);
        }
        else if (powerone < powertwo) {
            LEFTOVER = credits - (answer * powerone);
        }
        else if (powerone == powertwo) {
            LEFTOVER = credits - (answer * powerone);
        }
    }

    printf(" You can purchase a maximum of %d powerups, with %d credits left over.\n", answer, LEFTOVER);
    return 0;
}

2 个答案:

答案 0 :(得分:4)

你应该看一下C中的变量范围

每次你写int answer时,你都会创建(然后通常会丢弃)一个新变量,该变量恰好与更高范围的变量同名。

int answer = 1;
if (true)
{
    int answer = 2; // Seperate variable, which disappears at the close brace
}
// answer still equals 1

int LEFTOVER= 1;
if (true)
{
    LEFTOVER= 2; // Correct! Accessing LEFTOVER that already exists.
}
// LEFTOVER now equals 2

随机数是因为如果你只是声明一个变量而没有在C和C ++(int answer;)中设置一个值,那么它只会事先在内存中留下任何随机位。

在您正在进行的编码级别上,使用合理的默认值或明显的错误代码(如int answer = -1)来初始化变量几乎总是值得。

答案 1 :(得分:0)

#include <stdio.h>

int main()
{
//listen for cost of first powerup
    int powerone;
    printf("How much does the first powerup cost (in credits)?\n");
    scanf("%d", &powerone);
//listen for cost of second powerup
    int powertwo;
    printf("How much does the second powerup cost (in credits)?\n");
    scanf("%d", &powertwo);
//listen number of credits
    int credits;
    printf("How many credits do you currently have?\n");
    scanf("%d", &credits);
//figure out how many powerups you can get
   int answer = 0;
   if (powerone > powertwo) {
        answer = credits / powertwo;
    }
    else if (powerone < powertwo) {
        answer = credits / powerone;
    }
    else if (powerone == powertwo) {
        answer = credits / powerone;
    }

    int LEFTOVER = 0;    
    if (powerone > powertwo) {
        LEFTOVER = credits - (answer * powertwo);
    }
    else if (powerone < powertwo) {
        LEFTOVER = credits - (answer * powerone);
    }   
    else if (powerone == powertwo) {
        LEFTOVER = credits - (answer * powerone);
    }    
printf(" You can purchase a maximum of %d powerups, with %d credits left over.\n", answer, LEFTOVER);

return 0;

}

由于您在所有条件中使用常见变量answer,因此无需再次声明它。您应该只声​​明一次,并且还应该将其初始化为0.与Leftover变量相同。

尝试运行它会起作用。