如何从java中的公共方法返回多个double类型变量?

时间:2015-04-30 05:37:44

标签: java variables object methods

我有一个公共方法,我希望将双变量传递给我的main方法,我再次调用它们:

(在实际代码中,多次调用public方法,并且需要将相同的值返回给调用者)

 public double ReturnDoubleVariables() 
  {
    double ret1 promptUserForInput("1st Varaible", );
    double ret2 promptUserForInput("2nd Varaible", );
    double ret3 promptUserForInput("3rd Varaible", );
    double ret4 promptUserForInput("4th Varaible", );
    double ret5 promptUserForInput("5th Varaible", )

    return (ret1, ret2,...,ret5);
   }


  private void execute0() {

   Simulation simulation_0 = getActiveSimulation();
        scalarProfile_0.getMethod(ConstantScalarProfileMethod.class).getQuantity().setValue(ret1);

        scalarProfile_0.getMethod(ConstantScalarProfileMethod.class).getQuantity().setValue(ret2);

        scalarProfile_0.getMethod(ConstantScalarProfileMethod.class).getQuantity().setValue(ret3);

        scalarProfile_0.getMethod(ConstantScalarProfileMethod.class).getQuantity().setValue(ret4);

        scalarProfile_0.getMethod(ConstantScalarProfileMethod.class).getQuantity().setValue(ret5);

   }

我在从公共方法返回多个变量时遇到问题,然后我不知道如何在main方法中调用变量?

提前致谢

5 个答案:

答案 0 :(得分:3)

您可以返回一个数组:

function traverse(node) {
                                                    // do something with node
                                                    console.log('***');
                                                    node.eachChild(function(child) {
                                                        traverse(child); // handle the child recursively
                                                    });
                                                }                                   

                                                traverse(treeStore.getRootNode()); // start tree traversal with root node

或:

 public double[] ReturnDoubleVariables() 
 {
    double ret1 = promptUserForInput("1st Varaible", );
    double ret2 = promptUserForInput("2nd Varaible", );
    double ret3 = promptUserForInput("3rd Varaible", );
    double ret4 = promptUserForInput("4th Varaible", );
    double ret5 = promptUserForInput("5th Varaible", )

    return new double[]{ret1, ret2, ret3, ret4, ret5};
  }

答案 1 :(得分:2)

您可以返回Double值列表:

public List<Double> ReturnDoubleVariables() 
  {
    double ret1=promptUserForInput("1st Varaible", );
    double ret2=promptUserForInput("2nd Varaible", );
    double ret3=promptUserForInput("3rd Varaible", );
    double ret4=promptUserForInput("4th Varaible", );
    double ret5=promptUserForInput("5th Varaible", )
    List<Double> list = new ArrayList<Double>();
    list.add(ret1);    
    list.add(ret2);
    ...
    list.add(ret5);
    return list;
   }

或者您可以为reurn值创建一个类:

public class RetDouble{
   private double ret1;
   private double ret2;
   ... 
   private double ret5;
   //getter and setter;
}
public RetDouble ReturnDoubleVariables() 
  {
    double ret1=promptUserForInput("1st Varaible", );
    double ret2=promptUserForInput("2nd Varaible", );
    double ret3=promptUserForInput("3rd Varaible", );
    double ret4=promptUserForInput("4th Varaible", );
    double ret5=promptUserForInput("5th Varaible", );

    RetDouble r = new RetDouble();
    r.setRet1(ret1);
    r.setRet2(ret2);
    ...
    r.setRet5(ret5);
    return r;
   }

答案 2 :(得分:1)

您可以返回 let service = Service.sharedInstance service.requestDocuments() 之类的

double[]

此外,Java方法名称以小写字母开头(按惯例),因此上述内容应为public double[] ReturnDoubleVariables() { return new double[] { promptUserForInput("1st Varaible"), promptUserForInput("2nd Varaible"), promptUserForInput("3rd Varaible"), promptUserForInput("4th Varaible"), promptUserForInput("5th Varaible") }; }

答案 3 :(得分:1)

将它们捆绑在一个对象中,最好是List<Double>(除非您事先知道要返回多少个,然后使用一个简单的数组)。

列出示例

public List<Double> ReturnDoubleVariables() {

     List<Double> list = new ArrayList<Double>();

     //consider looping instead...
     list.add(promptUserForInput("1st Varaible", ));
     list.add(promptUserForInput("2nd Varaible", ));
     list.add(promptUserForInput("3rd Varaible", ));
     list.add(promptUserForInput("4th Varaible", ));
     list.add(promptUserForInput("5th Varaible", ));

     return list;

}

数组示例

public double[] ReturnDoubleVariables() {

     double[] arr = new double[5];

     //consider looping instead...
     arr[0] = list.add(promptUserForInput("1st Varaible", );
     arr[1] = promptUserForInput("2nd Varaible", );
     arr[2] = promptUserForInput("3rd Varaible", );
     arr[3] = promptUserForInput("4th Varaible", );
     arr[4] = promptUserForInput("5th Varaible", );

     return arr;

}

答案 4 :(得分:0)

使用Array返回,或使用ListArrayList,LinkedList),Sets等集合

public double[] ReturnDoubleVariables() {
return new double[] { promptUserForInput("1st Varaible"),
        promptUserForInput("2nd Varaible"),
        promptUserForInput("3rd Varaible"),
        promptUserForInput("4th Varaible"),
        promptUserForInput("5th Varaible") };

}

public ArrayList<Double> ReturnDoubleVariables() {
ArrayList<Double> destinationArray = new ArrayList<Double>();
destinationArray.add("1st Varaible")
destinationArray.add("2nd Varaible");
destinationArray.add("3rd Varaible");
return destinationArrayList;

}