如何在Java中返回多个变量

时间:2017-09-21 07:24:09

标签: java return

我需要返回多个变量,但我很困惑如何做到这一点。 我只知道返回1个变量。

Tyear,aveannualcost,aveinv,aveorderperyear,avelostdemandperyear,avelostdemandperyear

public class inventory {

    DistDiskrit acak = new DistDiskrit();

    float inisialisasi (float Tfinal, float s, float Q, float dist_x) {
        float TT = 1, demand1;
        float cumlost = 0, Norder = 0, TOrderArrive = 0 ;
        boolean Orderplaced = false;
        float inv = 20, cuminv = inv, Cinv = (float) 0.4, Corder = 5, Cpenalty = 1;
        while(TT < Tfinal) {
            if(TT == TOrderArrive){     
                inv = inv + Q;
                Orderplaced = false;
            }
            cuminv = cuminv + inv;
            demand1 = demand(TT);
            if(demand1 > inv) {
                cumlost = (float) (cumlost + demand1 - inv);
                inv = 0;
            }                
            else {inv = inv - demand1;}
            if((inv <= s) & (!Orderplaced))  
                Orderplaced = true;
            TOrderArrive = TT + 4 + (dist_x * 3);
            Norder = Norder + 1;
            TT = TT + 1;
        }
        float Tyear = Tfinal/365;
        float aveannualcost=Cinv*(cuminv/TT)+(Corder/Tyear)*Norder+Cpenalty*(cumlost/Tyear);
        float aveinv = cuminv/TT;
        float aveorderperyear = Norder/Tyear;
        float avelostdemandperyear = cumlost/Tyear;
        return avelostdemandperyear;
    }
    float mean (int n) {
        float x[] = new float[n];
        float jumlah=0;
        for(int i=0;i<n;i++){
            jumlah = jumlah+x[i];
        }
        return jumlah/n;
    }
}

[更新部分]

所以,我的朋友告诉我要添加这样的东西

   public float [] inisialisasi (float Tfinal, float s, float Q, float dist_x){
.
.
.    
    float rtrn []= {Tyear,aveannualcost, aveorderperyear, avelostdemandperyear};
            return rtrn;
}

我得到了这个 - [F @ d93b30

1 个答案:

答案 0 :(得分:-1)

Java方法只能返回一个东西。然而,那个东西可以是标量或对象。所以你可以创建一个类来保存你计算的变量并返回它。

例如:

class Example {
    float Tyear;
    float aveannualcost;
    float aveinv;
    float aveorderperyear;
    float avelostdemandperyear;
}

你需要使用构造函数和/或访问器方法来充实它。