为什么要返回垃圾?

骑士

我正在为游戏制作计算器。
通常,我通常先确保代码在C ++中运行,然后将其转换为所需的语言。我做了这个程序。在此程序中,有时候,optDura存储正确的值,而有时,它存储垃圾。

这是代码:-

#include <iostream>
using namespace std;

/* 
    cd - current Durability
    se - smith efficiency in decimal
    sc - smith charges in decimal
    sE - smith efficiency in %
    sC - smith charges in %
    ic - initial cost
    md - maximum durability
    tmd - temporary maximum durability
    tD - temporary durability
    td - total durability
    rc - repair cost
    cpb - cost per battle
*/
int main(){

    long int currDura, maxDura, tempMaxDura, tempDura, totDura, optDura;
    double iniCost, repCost;
    long int  smithEffi, smithCharge;
    double se, sc;
    double totCostTillNow, costPerBattle = 0, minCPB;
    int i;
    long int repCount = 1;
    iniCost = 25500;
    currDura = 58;
    maxDura = 59;
    repCost = 27414;
    se = 0.90;
    sc = 0.85;
    tempMaxDura = maxDura;
    tempDura = currDura;
    totDura = tempDura;
    totCostTillNow = iniCost;
    costPerBattle = totCostTillNow / totDura;
    minCPB = costPerBattle;
    cout<<"\n Cost without any repair = "<<costPerBattle;

    for(i=1; i<=maxDura; i++)
    {
        totCostTillNow += (double) repCost * sc;
        tempDura = tempMaxDura * se;
        totDura += tempDura;
        costPerBattle = (double) (totCostTillNow / totDura);
        tempMaxDura -= 1;
        if ( minCPB >=  costPerBattle )
        {
            minCPB = costPerBattle;
            optDura = tempMaxDura;
        }
    }
    cout<<"\n Minimum cost per battle = "<<minCPB<<" & sell at 0/"<<optDura;
    return 0;
}

当repCost> = 27414时,会发生此问题。对于小于该值的值,它将提供所需的结果。我无法弄清楚为什么会这样。

非常感谢

莫尔布德尼洛

如果重写以初始化变量,而不是使用“声明并分配”反“模式”(我也删除了未使用的变量):

int main(){  
    long int currDura = 58;
    long int maxDura = 59;
    long int tempMaxDura = maxDura;
    long int tempDura = currDura;
    long int totDura = tempDura;
    long int optDura; 
    double iniCost = 25500;
    double repCost = 27414;
    double se = 0.90;
    double sc = 0.85;
    double totCostTillNow = iniCost;
    double costPerBattle = totCostTillNow / totDura;
    double minCPB = costPerBattle;

    cout<<"\n Cost without any repair = "<<costPerBattle;

    for(int i=1; i<=maxDura; i++)
    {
        totCostTillNow += repCost * sc;
        tempDura = tempMaxDura * se;
        totDura += tempDura;
        costPerBattle = totCostTillNow / totDura;
        tempMaxDura -= 1;
        if ( minCPB >=  costPerBattle )
        {
            minCPB = costPerBattle;
            optDura = tempMaxDura;
        }
    }
    cout<<"\n Minimum cost per battle = "<<minCPB<<" & sell at 0/"<<optDura;
    return 0;
}

然后optDura突出显示为唯一缺少的初始化方法。
唯一为其分配值的是if minCPB >= costPerBattle,因此,如果该条件永远不成立,则您将获得不确定的值。

将其初始化为明智的选择。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么要打印多余的垃圾字符?

来自分类Dev

为什么要打印垃圾值?

来自分类Dev

为什么此代码返回垃圾值?

来自分类Dev

为什么函数返回垃圾值?

来自分类Dev

为什么要禁用垃圾收集器?

来自分类Dev

为什么 Java 垃圾回收要批量执行?

来自分类Dev

为什么要返回(0 * ap ++)?

来自分类Dev

为什么要返回新的静态?(PHP)

来自分类Dev

为什么我的函数在应返回char时返回垃圾?

来自分类Dev

为什么我的函数在应返回char时返回垃圾?

来自分类Dev

为什么要进行垃圾收集?为什么编译器不自动插入free()?

来自分类Dev

返回(空)而不是返回空-为什么要加上括号?

来自分类Dev

为什么要“返回”;跳过$ .each()中的迭代?

来自分类Dev

为什么要返回触发器(“点击”)?

来自分类Dev

scikit为什么要学习返回日志密度?

来自分类Dev

在Python中,何时/为什么要返回函数?

来自分类Dev

为什么要返回,如果否则不起作用

来自分类Dev

为什么要返回这个值?C ++ int / char混淆

来自分类Dev

在Javascript中,为什么要返回;什么时候什么都不返回?

来自分类Dev

为什么要令牌?

来自分类Dev

为什么要打印?

来自分类Dev

为什么要麦迪逊?

来自分类Dev

为什么要串联?

来自分类Dev

为什么要调用?

来自分类Dev

为什么要令牌?

来自分类Dev

为什么要内联?

来自分类Dev

为什么显示垃圾值?

来自分类Dev

为什么不输出“垃圾”?

来自分类Dev

为什么glGetProgramiv GL_ACTIVE_UNIFORMS偶尔返回垃圾并使程序崩溃?