当前位置: 首页 > 知识库问答 >
问题:

我正在尝试创建一个类,它将显示关于一组整数的信息,但我不能调用我的类

萧业
2023-03-14
package com.company;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        //int sum, even, odd, min, max, size;
        //int [] intArray;
        //double average;
        //int even = 0, odd = 0, min = 0, max = 0, size = 0, sum = 0;
        //double average;
        System.out.print("Please enter a number for the array size (zero or greater):  ");
        int n=in.nextInt();
        if (n<0){
            System.out.println("ERROR: Number must be at least zero!!!");
        }else {
            IntArray mainArray = new IntArray(n);
        }
        //com.company.IntMath intMath = new com.company.IntMath(average, even, odd, min, max, size, sum);
        IntMath intMath = new IntMath();
        //intMath.getAverage();
        intMath.getEven();
        intMath.getOdd();
        intMath.getMin();
       // intMath.getMax();
        intMath.getSize();
        intMath.getSum();

        System.out.println("Average: " + intMath.getAverage() );  //print the average of the elements
        System.out.println("Even Count: " + intMath.getEven()); //prints the count of all even numbers
        System.out.println("Odd Count: " + intMath.getOdd()); //prints the count of all odd numbers
        System.out.println("Min: " + intMath.getMin()); //prints the min number
        //System.out.println("Max: " + intMath.getMax()); //prints the max number
        System.out.println("Size: " + intMath.getSize()); //prints the size of the array
        System.out.println("Sum: " + intMath.getSum()); //prints the sum of all elements

    }
}
package com.company;

import java.util.Arrays;
import java.util.Random;

public class IntArray {
    private int n;
    private int [] intArray;
    private double average;
    private int sum;
    private int even;
    private int odd;
    private int max;
    private int min;
    private int size;

    public IntArray(int n) {
        this.n = n;

        Random randArray = new Random();
        int[] intArray = new int[n];
        intArray[0] = 0; //why does this not make the element 0 a 0?????????
        for (int i = 0; i < n; i++) {
            intArray[i] = randArray.nextInt(50); //I was getting very big random numbers, so I set the max to 50
        }
        System.out.println("Set: " + Arrays.toString(intArray));


    }
    public double getAverage() {
        average = 0;
        double total = 0;
        for (int i = 0; i < n; i++) {
            total = total + intArray[i];
        }
        return average;
    }

    public int getSum() {
        //find the sum of all elements
        sum = 0;
        for (int i = 0; i < n; i++) {
            sum += intArray[i];
        }
        return sum;
    }

    public int getEven() {
        even = 0;
        for (int i = 0; i < n; i++) {
            if (intArray[i] % 2 == 0) {
                even++;
            }
        }
        return even;
    }

    public int getOdd() {
        odd = 0;
        for (int i = 0; i < n; i++) {
            if (intArray[i] % 2 != 0) {
                odd++;
            }
        }
        return odd;
    }

    public int getMax() {
        max = intArray[0];
        for (int i = 1; i < n; i++) {
            if (intArray[i] > max) {
                max = intArray[i];
            }
        }
        return max;
    }

    public int getMin() {
        min = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < intArray.length; j++) {
                if (intArray[i] > intArray[j]) {
                    min = intArray[i];
                    intArray[i] = intArray[j];
                    intArray[j] = min;
                }
            }
        }
        return min;
    }

    public int getSize() {

        //find the size of the array
        size = n + 1;
        return size;
    }

}
package com.company;
public class IntMath {
    private double average;
    private int sum;
    private int even;
    private int odd;
    private int max;
    private int min;
    private int size;
    private int[] intArray;
    private int n;

    //com.company.IntArray mainArray = new com.company.IntArray(n); //call IntArray to get variables ''n'' and ''intArray''


    /*public IntMath(double average, int sum, int even, int odd, int max, int min, int size, int[] intArray) {
        this.average = average;
        this.sum = sum;
        this.even = even;
        this.odd = odd;
        this.max = max;
        this.min = min;
        this.size = size;
        this.intArray = intArray;

        com.company.IntArray mainArray = new com.company.IntArray(n); //call IntArray to get variables ''n'' and ''intArray''

        // average = 0;
        // double total = 0;
        // for (int i = 0; i < n; i++) {
        //     total = total + intArray[i];
        // }


        //find the sum of all elements
        //sum = 0;
        //for (int i = 0; i < n; i++) {
        //    sum += intArray[i];
        //}

        //find the count of the even numbers
        //even = 0;
        //for (int i = 0; i < n; i++) {
        //    if (intArray[i] % 2 == 0) {
        //        even++;
        //    }
        //}

        //find the count of the odd numbers
        //odd = 0;
        //for (int i = 0; i < n; i++) {
        //    if (intArray[i] % 2 != 0) {
        //        odd++;
        //    }
        //}

        //find the biggest number
        //max = intArray[0];
        //for (int i = 1; i < n; i++) {
        //    if (intArray[i] > max) {
        //        max = intArray[i];
        //    }
        //}

        //find the smallest number
        //min = 0;
        //for (int i = 0; i < n; i++) {
        //    for (int j = i + 1; j < intArray.length; j++) {
        //        if (intArray[i] > intArray[j]) {
        //            min = intArray[i];
        //            intArray[i] = intArray[j];
        //            intArray[j] = min;
        //        }
        //    }
    /

    //find the size of the array
    //size = n + 1;
*/

    public double getAverage() {
        average = 0;
        double total = 0;
        for (int i = 0; i < n; i++) {
            total = total + intArray[i];
        }
        return average;
    }

    public int getSum() {
        //find the sum of all elements
        sum = 0;
        for (int i = 0; i < n; i++) {
           sum += intArray[i];
        }
        return sum;
    }

    public int getEven() {
        even = 0;
        for (int i = 0; i < n; i++) {
            if (intArray[i] % 2 == 0) {
                even++;
            }
        }
        return even;
    }

    public int getOdd() {
        odd = 0;
        for (int i = 0; i < n; i++) {
            if (intArray[i] % 2 != 0) {
                odd++;
            }
        }
        return odd;
    }

    public int getMax() {
        max = intArray[0];
        for (int i = 1; i < n; i++) {
            if (intArray[i] > max) {
                max = intArray[i];
            }
        }
        return max;
    }

    public int getMin() {
        min = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < intArray.length; j++) {
                if (intArray[i] > intArray[j]) {
                    min = intArray[i];
                    intArray[i] = intArray[j];
                    intArray[j] = min;
                }
            }
        }
        return min;
    }

    public int getSize() {

        //find the size of the array
        size = n + 1;
        return size;
    }
}


共有1个答案

上官季
2023-03-14

在类Main中,如果初始化所有变量,该类将在编译时不会出错。

通过你的课程,我想你可能想做的是,

  • 接受一个数字以创建该大小的数组,并使用一些随机数填充该数组
  • 接下来,您要使用已填充的数组执行各种操作,如求平均值、求和等。
    null
 类似资料: