我正在尝试创建3种方法来计算随机数组的和和平均值,然后输出结果。
我正在尝试获得与-类似的输出 Java RandomArray 5 9 7 2 1 4 总计:23 均值:4.6
但是我收到“用法:java RandomArray。示例:java RandomArray 5”
如果您可以在我的代码中发现错误并帮助获取此输出。
public class RandomArray {
private int[] numbers; //instance variable
/**
* Constructor
*
*@param size 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(i + " ");
}
/**
* A method to calculate the sum of all elements
*
*@return The sum
*/
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();
}
}
答案 0 :(得分:0)
您的方法“ printArray(int sum)”有错误。调用该方法时,您将不需要参数总和。然后,您尝试使用名称sum创建第二个局部变量。这是不可能的,因为您已经有一个名为sum的参数。
在for循环中,您初始化了一个名为sum的局部变量。您需要为该变量使用其他名称,因为您只需要使用它来计数循环。
因此,删除参数sum并重命名for循环中使用的变量。
这应该有效:
import java.util.Arrays;
public class ArrayCalculations
{
private int[] numbers;
public ArrayCalculations(int size)
{
this.numbers = new int[size];
for (int i = 0; i < size; i++)
{
int randomNumber = (int) (Math.random() * 10);
numbers[i] = randomNumber;
}
}
public int calculateSum()
{
int sum = 0;
for (int i = 0; i < numbers.length; i++)
{
sum += numbers[i];
}
return sum;
}
public float calculateAverage()
{
float sum = (float) calculateSum();
return sum / numbers.length;
}
public void printArray()
{
System.out.println(Arrays.toString(numbers));
}
public static void main(String[] args)
{
ArrayCalculations ac = new ArrayCalculations(5); // initiates an array with 5 elements
ac.printArray(); // prints the array to console
System.out.println("The sum is: " + ac.calculateSum()); // prints the sum
System.out.println("The average is: " + ac.calculateAverage()); // prints the average
}
}
答案 1 :(得分:0)
让我们先看看您的sum方法:
public int calSum(int sum) {
int sum = 0;
for (int value : numbers) {
sum += value;
}
return sum;
}
这是一个清理后的版本:
public int calSum() {
int sum = 0;
for (int value : numbers) {
sum += value;
}
return sum;
}
接下来,平均水平:
public double calMean(int[] array){
//Unsure which elements go here
}
public double calMean() {
int sum = calSum();
int length = numbers.length;
return (double) sum / length;
}
请注意此处使用(double)
。没有它,cal / length
将是整数除,结果将四舍五入到最接近的整数。