[英]calling another method from different class
I was practicing java.我在练习 java。 And I created a method in another class within the same package such that,我在同一个 package 中的另一个 class 中创建了一个方法,这样,
class ArraysPractice2{
int n;
int[] arr = new int[n];
double averageA(int [] arr)
{
double ans = 0;
int added = 0;
int total = arr.length;
for(int i = 0; i < arr.length; i++)
{
added = added + arr[i];
}
ans = added / total;
return ans;
}
}
and then I have a main method has following code,然后我有一个主要方法有以下代码,
ArraysPractice2 aT = new ArraysPractice2();
int[] testArr = new int[10];
for(int i = 0; i < testArr.length; i++)
{
testArr[i] = i + 1;
}
aT.averageA(testArr);
I expected that aT.averageA
would give me the average of testArr
array.我希望aT.averageA
会给我testArr
数组的平均值。 However, it does not give me anything.但是,它并没有给我任何东西。
What are the issues in here?这里有什么问题?
I have a feeling that I am not understanding OOP well..我有一种感觉,我对 OOP 不太了解..
Thank you for your help.谢谢您的帮助。
aT.averageA(testArr);
You are not receiving/using a value you have got in the line above.您没有收到/使用您在上面一行中获得的值。
Try this:尝试这个:
double res = aT.averageA(testArr);
System.out.println("aT.averageA(testArr = "+res);
and you should see your value.你应该看到你的价值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.