【问题标题】:Issue with arrays and output数组和输出问题
【发布时间】: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


【解决方案1】:
public class Hello {

    public static void main(String args[]) {

        System.out.println("Random generated array\n-----------------------");
        int[] test = displayArr();

        double avg = findMean(test);
        System.out.println("\nAverage " + avg);

        int range = findRange(test);
        System.out.println("Range " + range);

        System.out.println(
        "\nRandom generated array every value multiplied by 2 \n-----------------------------------------------------");
        doubleEveryOtherVal(test);


    }

    // first method
    public static int[] displayArr() {
        // loop to generate 10 random numbers
        int[] test = new int[10];
        for (int i = 0; i < 10; i++) {
            // Randomly generated doubles
            int random = (int) (Math.random() * 50 + 10);
            test[i] = random;
        }

        // outputs the randomly generated numbers
        for (int d : test) {
            System.out.println(d);
        }

        // returns the array
        return test;
    }

    // second method
    public static double findMean(int[] arr) {
        // begin sum
        int sum = 0;
        // sum the values of the array
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
            // compute average
        }

        double avg = sum / arr.length;

        return avg;
    }

    // third method
    public static int findRange(int[] arr) {
        int range = 0;

        // find max
        int max = arr[0];

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }

        // find the min.
        int min = arr[0];

        for (int x = 0; x < arr.length; x++) {
            if (arr[x] < min) {
                min = arr[x];
            }

        }

        // max-min =range
        range = max - min;

        return range;
    }

    // final method
    public static void doubleEveryOtherVal(int[] arr) {
        // create a loop that only iterates for every other value
        for (int i = 0; i < arr.length; i++) {
            // extract every other value from the array
            arr[i] = arr[i] * 2;
        }

        for(int doubled : arr)
        {
            System.out.println(doubled);
        }

    }
}

您使用的代码中有很多错误。首先,我不知道您为什么在名为 math 的方法中使用第二个参数。另一个问题是您不能在返回 void 的方法上调用 System.out.println()。

我还建议您对数组和任何有 int 的地方都使用 double 以获得更精确的值。

最后一种方法我不太理解,但我认为您的意思是它应该将数组中找到的每个值加倍(另一个原因是您应该始终使用 double 以获得更精确的值)。

这是使用 Junit 测试进行简单测试的方法。

import static org.junit.Assert.*;

import org.junit.Test;

public class TestHello {

    @Test
    public void testAssertions() {
        Hello h1 = new Hello();

        // testing that the array returned has a length of 10
        assertTrue(h1.displayArr().length == 10);
    }

}

有关 JUnit 测试的更多信息,请访问此链接

http://www.tutorialspoint.com/junit/junit_test_framework.htm

希望你找到这个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    相关资源
    最近更新 更多