我这里有两个类,一个用于制作数组,另一个用于打印它们。在尝试编译打印时,它无法识别数组获取方法。我究竟做错了什么?这是程序:
public class IntegerArrayMain
{
private int[] numbers = new int [25];
private int[] evens = new int [25];
private int[] odds = new int [25];
private int[] negatives = new int [25];
private int sum;
private int numbersSum;
private int evenSum;
private int oddSum;
private int negaSum;
private int avg;
private int numbersAvg;
private int evenAvg;
private int oddAvg;
private int negaAvg;
DiceClass rng = new DiceClass(200);
//============================NUMBERS====================================
/**
*Generates the 25 numbers.
*/
public int[] setNumbers()
{
for(int i = 0; i < numbers.length; i++)
{
numbers[0] = (rng.getRoll() - 100);
sum += numbers[i];
}
return numbers;
}
/**
*Returns the numbers.
*/
public int[] getNumbers()
{
return numbers;
}
/**
*Finds and returns the sum of the numbers.
*/
public int setNumsSum()
{
int numbersSum = 0;
for( int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
return numbersSum;
}
/**
*Returns the numbers' sum.
*/
public int getNumsSum()
{
return numbersSum;
}
/**
*Finds and returns the average of the numbers.
*/
public double setNumsAvg()
{
double numbersAvg = sum/numbers.length;
return numbersAvg;
}
/**
*Returns the numbers' average.
*/
public int getNumsAvg()
{
return numbersAvg;
}
//============================EVENS======================================
/**
*Locates the even numbers.
*/
public void findEvens()
{
int n = 0;
for(int i : numbers){
if((i % 2) == 0){
evens[n] = i;
}
n++;
}
}
/**
*Returns the even numbers.
*/
public int[] getEvens()
{
return evens;
}
/**
*Finds and returns the sum of the evens.
*/
public int setEvensSum()
{
int evenSum = 0;
for( int i = 0; i < evens.length; i++) {
sum += evens[i];
}
return evenSum;
}
/**
*Returns the even numbers' sum.
*/
public int getEvensSum()
{
return evenSum;
}
/**
*Finds and returns the average of the evens.
*/
public double setEvensAvg()
{
double evenAvg = sum/evens.length;
return evenAvg;
}
/**
*Returns the even numbers' average.
*/
public int getEvensAvg()
{
return evenAvg;
}
//============================ODDS=======================================
/**
*Locates the odd numbers.
*/
public void findOdds()
{
int n = 0;
for(int i : numbers){
if((i % 1) == 0){
odds[n] = i;
}
n++;
}
}
/**
*Returns the odd numbers.
*/
public int[] getOdds()
{
return odds;
}
/**
*Finds and returns the sum of the odds.
*/
public int setOddsSum()
{
int oddSum = 0;
for( int i = 0; i < odds.length; i++) {
sum += odds[i];
}
return oddSum;
}
/**
*Returns the odd numbers' sum.
*/
public int getOddsSum()
{
return oddSum;
}
/**
*Finds and returns the average of the odds.
*/
public double setOddsAvg()
{
double oddAvg = sum/odds.length;
return oddAvg;
}
/**
*Returns the odd numbers' average.
*/
public int getOddsAvg()
{
return oddAvg;
}
//============================NEGATIVES==================================
/**
*Locates the negative numbers.
*/
public void findNegatives()
{
int n = 0;
for(int i : numbers){
if((i % 2) == 0){
negatives[n] = i;
}
n++;
}
}
/**
*Returns the negative numbers.
*/
public int[] getNegatives()
{
return negatives;
}
/**
*Finds and returns the sum of the negatives.
*/
public int setNegativesSum()
{
int negaSum = 0;
for( int i = 0; i < negatives.length; i++) {
sum += negatives[i];
}
return negaSum;
}
/**
*Returns the negatives numbers' sum.
*/
public int getNegativesSum()
{
return negaSum;
}
/**
*Finds and returns the average of the negatives.
*/
public double setNegativesAvg()
{
double negaAvg = sum/negatives.length;
return negaAvg;
}
/**
*Returns the negative numbers' average.
*/
public int getNegativesAvg()
{
return negaAvg;
}
}
另一个:
import java.util.Scanner;
public class IntegerArrayRunner
{
public static void main(String[] args)
{
System.out.println("Numbers Array: " + getNumbers());
System.out.println("Numbers Array Element Count: " + numbers.length());
System.out.println("Numbers Array Sum: " + getNumsSum());
System.out.println("Numbers Array Average: " + getNumsAvg());
System.out.println("Evens Array: " + getEvens());
System.out.println("Evens Array Element Count: " + evens.length());
System.out.println("Evens Array Sum: " + getEvensSum());
System.out.println("Evens Array Average: " + getEvensAvg());
System.out.println("Odds Array: " + getOdds());
System.out.println("Odds Array Element Count: " + odds.length());
System.out.println("Odds Array Sum: " + getOddsSum());
System.out.println("Odds Array Average: " + getOddsAvg());
System.out.println("Negatives Array: " + getNegatives());
System.out.println("Negatives Array Element Count: " + negatives.length());
System.out.println("Negatives Array Sum: " + getNegativesSum());
System.out.println("Negatives Array Average: " + getNegativesAvg());
}
}
答案 0 :(得分:1)
您需要创建该类的实例,然后使用该引用来调用方法。
public static void main(String[] args)
{
IntegerArrayMain integerArrayMain = new IntegerArrayMain;
integerArrayMain.callMethod1();
integerArrayMain.callMethod2();
//etc...
}
或者,您可以将方法和类变量设置为静态,然后使用ClassName.MethodName()
调用它