【发布时间】:2016-03-09 00:24:25
【问题描述】:
我有一段代码将派生一个随机数组,然后在该方法中进行各种计算。数组将返回内存,然后在 main 方法中我必须编写一个测试用例来展示这些方法是如何工作的。我不明白如何编写测试用例。 该数组是一个随机数组,设计为具有 10 个介于 1 和 999 之间的随机数,以下方法将每隔一倍计算平均值并计算范围。 我有以下代码,由于 main 方法中的错误,它不会编译。我该如何解决?
//main method test class
public static void main(String[] args) {
System.out.prntln(displayArr);
System.out.println(findMean);
System.out.pritnln(findRange);
System.out.println(doubleEveryOther);
}
//first method
public static void displayArr(int [] arr) {
//loop to generate 10 random numbers
for(int i = 0; i<10; i++) {
double math = 1+ (int) Math.random() *999;
}
//create an array of 10 generated random numbers
int [] math = new int [10];
System.out.println(math);
}
//second method
public static double findMean (int[] arr, int [] math) {
// begin sum
int sum =0;
//sum the values of the array
for (int i=0; i<math.length; i++) {
sum = 0+math[i];
//compute average
}
double avg = sum/math.length;
return avg;
}
//third method
public static int findRange (int[] arr,int [] math) {
//find the max
int max =math[0];
for (int i=1; i<math.length; i++) {
if(math[i] >max) {
max=math[i];
}
}
// find the min.
int min = math[0];
for(int j=1; j<math.length; j++) {
if(math[j]<min) {
min = math[j];
}
}
//max-min =range
int range = max-min;
return range;
}
//final method
public static void doubleEveryOtherVal(int[] arr,int [] math) {
//create a loop that only iterates for every other value
for (int i=1; i<math.length; i+=2){
//extract every other value from the array
double odd = math[i];
//compute
double twice = 2 *odd;
}
}
【问题讨论】:
-
你在问什么?为什么你的代码无法编译?如何编写测试?如何为随机数据编写测试?
-
我在第三行看到
prntln而不是println。检查编译器输出,它应该告诉你错误在哪里。还有sum = 0+math[i];这一行应该说sum = sum+math[i];我假设
标签: java arrays loops methods call