我的任务是计算用户在数组中定义的整数的平均值,并使用我的类文件中的方法打印回 main。
目前我有两个文件要编译,但似乎无法弄清楚为什么什么都没有打印。请帮忙,不胜感激!
测试程序中要检查的代码片段:
// Print average of integers in array.
System.out.println("\nAverage of values in array =");
myArray.avgArray();
类程序中要检查的代码片段:
// 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();
}
}
这段代码包含我的类以及我将在 main 中实现的各种方法:
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;
}
这是运行时程序的示例输出:
----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.