java - 打印数组方法

标签 java arrays printing sum mean

我正在尝试打印我的方法 calSum 和 calMean。我想得到类似的输出:

java RandomArray 8

0 9 5 3 5 6 0 8

总和:36

平均值:4.5

但我得到的是 -Usage: java RandomArray 。示例:java RandomArray 5

我在 printArray 方法中做错了什么吗?或者是别的什么?任何有关我的代码中的错误的帮助都会很棒。

    public class RandomArray {

private int[] numbers; //instance variable

/**
 *  Constructor
 *
 *The size of the array
 */
public RandomArray(int size){
    numbers = new int[size];
    for(int i=0; i<numbers.length;i++){
        numbers[i] = (int)(Math.random()*10); // a random number between 0-9
    }
}

/**
 *  a method to print the array elements
 */

public void printArray() {
    for (int i = 0; i < numbers.length; i++)
        System.out.print("Java Random array:"+ numbers);
        System.out.println("Sum:" + calSum());
        System.out.println("Mean:" + calMean());
}       

/**
 *  A method to calculate the sum of all elements
 *
 */
public int calSum(){
 int sum = 0;
 for (int value : numbers) {
     sum += value;
}
    return sum;

}

/**
 *  A method to calculate the mean of all elements
 *
 *@return    The mean
 */

public double calMean() {
    int sum = calSum();
    int length = numbers.length;

    return (double) sum / length;
}


/**
 *  a method to print the array elements in reverse order
 */
public void printReverse(){


}

/**
 *  A main method to test
 */
public static void main(String[] args) {
    // Check to see if the user has actually sent a paramter to the method
    if (args.length != 1){
        System.out.println("Usage: java RandomArray <NUM>. Example: java RandomArray 5");
        System.exit(-1);
    }

    // Create an instance of the class 
    RandomArray test = new RandomArray(Integer.parseInt(args[0]));


    // Print the array
    test.printArray();

    // Calculate the sum of all the values in the array and print it
    System.out.println("Sum: "+ test.calSum());

    // Calculate the mean of all the values in the array and print it
    System.out.println("Mean: "+ test.calMean());

    System.out.print("Reverse: ");
    test.printReverse();
}

 }

最佳答案

当您运行主类时,main方法接受String类型的元素数组。

您必须记住,该调用看起来像 java RandomArray arg1 arg2,即使您在 IDE 中运行它也是如此。该数组包含 java 之后的所有元素,甚至包括 RandomArray

因此 args 将始终由至少 1 个元素组成。 如果您想要值 arg1,则需要获取 args[1] 而不是 args[0]

您的代码应如下所示:

public static void main(String[] args) {
    // Check to see if the user has actually sent a paramter to the method
    if (args.length != 2){
        System.out.println("Usage: java RandomArray <NUM>. Example: java RandomArray 5");
        System.exit(-1);
    }

    // Create an instance of the class 
    RandomArray test = new RandomArray(Integer.parseInt(args[1]));

...

接下来,打印生成数组时将不会得到预期的结果。检查评论中的链接以了解如何正确打印数组。

关于java - 打印数组方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53652624/

相关文章:

java - 系统属性为 null,即使它是在命令行中定义的

java - 在 Spring Controller 的操作参数中,序列化的 ajax 表单会被转换为什么类型的对象?

c++ - 从文本文件读取到数组

arrays - 为什么我收到有关 Delphi 不兼容类型(数组和动态数组)的错误?

c - 在 C 中打印这个数组

java - In-App Billing v3 可靠性缺陷

java - getBytes vs getBinaryStream vs getBlob 用于从 BLOB 列中获取数据

Java字符串数组: is there a size of method?

javascript - 使用 jQuery 从 html 表中动态选择列

python - 为什么 python 打印语句不在无限循环之上?