基本上我想做的是能够:
class MainFile {
public static void main(String args[]){
class2 TEObject = new class2();
int var1 = 1;
int var2 = 1;
int var3 = 1;
int sc = 1;
TEObject.method1(sc, var1, var2, var3);
double[][] somearray = class2.out(somearray);
System.out.println(somearray);
}
}
这是第二堂课:
public class class2 {
private double Mult;
public double method1(int sc, int var1, int var2, int var3)
{
double[][] somearray =
{{1,1,1,1,1,1,1,1,1,1,1,1},
{1,0.5,1,0.5,0.5,1,1,2,2,0.5,1,2},
{1,2,0.5,1,1,0.5,1,1,0.5,1,1,2},
{1,2,1,0.5,0.5,1,1,0.5,2,1,0.5,2},
{1,2,2,2,0.5,1,0.5,1,1,0.5,1,0.5},
{1,1,0.5,1,1,0.5,2,0.5,1,2,2,0.5},
{1,0.5,1,0.5,1,1,0.5,2,0.5,2,2,1},
{1,0.5,2,2,2,2,0.5,0.5,0.5,1,1,0.5},
{1,1,1,0.5,2,2,1,2,0.5,0.5,0.5,1},
{1,1,0.5,1,2,0.5,2,0.5,1,0.5,2,1},
{1,1,2,1,0.5,0.5,0.5,1,2,2,0.5,1},
{1,0.5,0.5,2,1,2,2,1,1,1,0.5,0.5},
};
Mult = somearray[sc][var1]*somearray[sc][var2]*somearray[sc][var3];
return Mult;//This needs to be returned for other methods in class2
}
public static double[][] out(double[][] somearray){
return somearray;
}
//Some more methods go here
我在这里遇到的问题是,当我尝试调用方法时,它要求我输入一个值,表示该类型不适用于参数。我不能把null放在那里,否则它只会打印null。
如果不这样做,我基本上想要一种能够从类输出到主类的方法,以便我可以在主类中操作数组。
编辑:我已经更新了课程,所以他们更清楚我要做什么。答案 0 :(得分:0)
您必须将method2
的返回值存储在Main
类的变量中,如下所示:
double[][] somearray = class2.object.method2();
如果要从somearray
中获取method1
,这不会起作用,因为您只能在Java方法中返回单个对象。唯一的出路是包含somedouble
和somearray
的包装器对象。或者,如果您不需要返回somedouble
,请选择somearray
。
答案 1 :(得分:0)
你正在调用没有参数的method2,但它期待一个参数 方法2也有一个您没有使用的返回值。退一步,调用代码的意图和class2的界面根本不适合。