我的任务是计算用户在数组中定义的整数的平均值,并使用我的类文件中的方法打印回main。
目前我有两个文件要编译,但似乎无法弄清楚为什么没有打印。请帮助,非常感谢!
测试程序中的代码片段,用于检查:
// Print average of integers in array.
System.out.println("\nAverage of values in array =");
myArray.avgArray();
Class Program中的代码片段,用于检查:
// Method to calculate average of integers in array.
public double avgArray()
{
int sum = 0;
for(int ctr = 0; ctr < limit; ctr++)
{
sum = sum + nrs[ctr];
}
double avg = sum /(double)limit;
return avg;
}
此代码是我的测试目的:
import java.util.Scanner;
public class P5test
{
public static void main(String[] args)
{
// Set up Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Input size of array desired.
System.out.print("Size of array: ");
int size = keyboard.nextInt();
// Set up object.
P5class myArray = new P5class(size);
// Allow user to fill array.
System.out.println("\nEnter data for array:");
myArray.fillArray();
// Print contents of array.
System.out.println("\nContents of array:");
myArray.printArray();
// Print average of integers in array.
System.out.println("\nAverage of values in array =");
myArray.avgArray();
// Print postive values in array.
System.out.println("\nPositive values in array:");
myArray.pvaluesArray();
// Sort contents of array in ascending order.
System.out.println("\nSorted Array:");
myArray.sortArray();
myArray.printArray();
}
}
此代码包含我的类,其中包含我将要实现的各种方法:
import java.util.Scanner;
public class P5class
{
// Constructor setting up empty array of size specified by user.
public P5class(int size)
{
limit = size;
nrs = new int[limit];
}
// Method to fill array.
public void fillArray()
{
// Set up Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
for(int ctr = 0; ctr < limit; ctr++)
{
System.out.print("Integer #" + (ctr+1) + ": ");
nrs[ctr] = keyboard.nextInt();
}
}
// Method to display contents of array.
public void printArray()
{
for(int ctr = 0; ctr < limit; ctr++)
{
System.out.println("Nrs[" + ctr + "] = " + nrs[ctr]);
}
}
// Method to calculate average of integers in array.
public double avgArray()
{
int sum = 0;
for(int ctr = 0; ctr < limit; ctr++)
{
sum = sum + nrs[ctr];
}
double avg = sum /(double)limit;
return avg;
}
// Method to examine if any positive values are present in array.
public void pvaluesArray()
{
int largest = nrs[0];
// Check to see if any other number in array is larger than first.
for(int ctr = 1; ctr < limit; ctr++)
{
if(nrs[ctr] > largest)
largest = nrs[ctr];
}
int sum = 0;
for(int ctr = 0; ctr < limit; ctr++)
{
sum = sum + nrs[ctr];
}
double average = (double)sum/limit;
for(int ctr = 0; ctr < limit; ctr++)
{
if(largest <= 0)
{
System.out.println("\nArray contains no positive integers.");
break;
}
else if (nrs[ctr] > 0)
{
System.out.println("Nrs[" + ctr + "] = " + nrs[ctr]);
}
}
}
// Method to sort array into ascending order.
public void sortArray()
{
for(int ctr0 = 0; ctr0 < limit-1; ctr0++)
{
// Make one pass of Bubble Sort.
for(int ctr = 0; ctr < limit-1; ctr++)
{
if(nrs[ctr] > nrs[ctr+1])
{
int temp = nrs[ctr];
nrs[ctr] = nrs[ctr+1];
nrs[ctr+1] = temp;
}
}
}
}
// Instance variables.
private int limit;
private int[] nrs;
}
以下是Run:
时程序的示例输出 ----jGRASP exec: java P5test
Size of array: 5
Enter data for array:
Integer #1: 0
Integer #2: -9
Integer #3: 4
Integer #4: 7
Integer #5: 2
Contents of array:
Nrs[0] = 0
Nrs[1] = -9
Nrs[2] = 4
Nrs[3] = 7
Nrs[4] = 2
Average of values in array =
Positive values in array:
Nrs[2] = 4
Nrs[3] = 7
Nrs[4] = 2
Sorted Array:
Nrs[0] = -9
Nrs[1] = 0
Nrs[2] = 2
Nrs[3] = 4
Nrs[4] = 7
----jGRASP: operation complete.
答案 0 :(得分:3)
它返回一个值,但你没有做任何事情,试试:
System.out.println("\nAverage of values in array = " + myArray.avgArray());
或者:
double avg = myArray.avgArray();
System.out.println("\nAverage of values in array = " + avg);
或许多其他方式之一。
或者,您可以使用return avg;
在方法中将其打印出来,而不是System.out.println(avg);
,而是将返回类型更改为void
。