同一程序每次返回不同的输出?

每次运行程序时,使用完全相同的值(直径为25,深度为5),array_multisort(array_column($array,2),SORT_ASC,$array); 会得到不同的值,我不确定为什么。

一些结果:

water_price

我不知道我是否正在处理内存中的值,彼此之间不能很好地交互,不刷新或发生什么。

为什么每次运行此程序时结果都不同?

$6.62256e+07 is the total cost.
$0 is the total cost.
$2.43411e-27 is the total cost.
sml11111 回答:同一程序每次返回不同的输出?

了解我们如何需要声明变量的开头,然后在您要求输入时将其存储在这些变量中,然后可以继续进行所需的计算。

#include <iostream>
#define PI 3.1416
#define WATER_COST 1.80

using std::cout;
using std::cin;
using std::endl;

int main() {

    float pool_diameter = 0.0;
    float pool_depth = 0.0;

    cout << "Enter the pool diameter: ";
    cin >> pool_diameter;
    cout << "\nEnter the pool depth: ";
    cin >> pool_depth;


    float pool_radius = pool_diameter / 2;
    float pool_volume_sq_inches = (PI * pool_radius * pool_radius * pool_depth) * 1728;
    float pool_gallons = pool_volume_sq_inches / 231;
    float water_price = (pool_gallons / 748) * WATER_COST;


    cout << "\n$" << water_price << " is the total cost." << endl;

    return 0;
}
,

您可能希望在声明后立即获取输入。

        float pool_diameter,pool_depth;

        cout << "Enter the pool diameter: ";
        cin >> pool_diameter;
        cout << "\nEnter the pool depth: ";
        cin >> pool_depth;

其余代码将按原样工作。

一种好的做法是像Omid-CompSci一样在这里初始化变量。

,

声明float pool_radius = pool_diameter / 2;在执行之前 cin >> pool_diameter;。每次都会使用默认值(垃圾值)来计算pool_radius,这是在不同运行中产生不同值的原因。

更改顺序。

本文链接:https://www.f2er.com/3162205.html

大家都在问