[英]Printing array method
I am trying to print out my methods calSum and calMean. 我正在尝试打印我的方法calSum和calMean。 I want to get a similar output to:
我想得到类似的输出:
java RandomArray 8
Java RandomArray 8
0 9 5 3 5 6 0 8 0 9 5 3 5 6 0 8
Sum: 36 总计:36
Mean: 4.5 平均值:4.5
but instead I am getting - Usage: java RandomArray . 但相反,我得到了-用法:java RandomArray。 Example: java RandomArray 5
示例:java RandomArray 5
Am I doing something wrong in the printArray method? 我在printArray方法中做错了吗? Or is it something else?
或者是别的什么? Any help with the errors in my code would be great.
对我代码中的错误的任何帮助都将非常有用。
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();
}
}
When you run the main class, the main
method accepts an array of elements of type String
. 运行主类时,
main
方法接受String
类型的元素数组。
You have to keep in mind the call looks like java RandomArray arg1 arg2
, even when you run it within an IDE. 您必须记住,即使在IDE中运行该调用,其外观也类似于
java RandomArray arg1 arg2
。 The array include all elements after java, even RandomArray
. 该数组包括java之后的所有元素,甚至包括
RandomArray
。
So args
will always be made of at least 1 element. 因此,
args
将始终由至少1个元素组成。 If you want the value arg1
, you need to get args[1]
not args[0]
. 如果需要值
arg1
,则需要获取args[1]
而不是args[0]
。
Your code should looks like: 您的代码应如下所示:
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]));
...
Next, you won't get the expected result when printing the generate array. 接下来,在打印generate数组时不会获得预期的结果。 Check the link in the comments to see how to properly print an array.
检查注释中的链接以查看如何正确打印阵列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.