简体   繁体   English

无法从类中调用方法

[英]Having trouble calling a method from a class

I'm trying to finish my project by calling the methods I've created in my class. 我正在尝试通过调用我在课堂上创建的方法来完成我的项目。 I'm getting an error "constructor in class cannot be applied to given types. 我收到一个错误“类中的构造函数不能应用于给定的类型。

Here is my main 这是我的主要内容

public static void main(String[] args) {
    double[] grades = {98.7, 77.9, 90, 83, 67, 33, 81, 90 };
    HomeworkGrades cls = new HomeworkGrades();

and this is my class 这是我的班级

public class HomeworkGrades {
    private double[] grades;
    double [] HWgrades = { 1, 2, 3, 4, 5, 6, 7, 8};

    public HomeworkGrades(double [] gradesEarned)
    {
        grades= gradesEarned;  
        for (int i =0; i<gradesEarned.length;i++)
            HWgrades =gradesEarned;
    }
     public double calcAverage()
    {
        int sum =0;
        for(int i =0; i<HWgrades.length; i++)
        {
            sum = (int) (sum + HWgrades[i]);
        }
        double average = sum / (double)HWgrades.length;
        return average;
    }
     public static  double calcLowestGrade(double[]a, double total)
     {
         double temp;
         double size;
         double  array[] = { 1, 2, 3, 4, 5, 6, 7, 8};
         size = array.length;
         for (int i =0; i <size; i++)
         {
             for(int j=i+1;j<size;j++)
             {
                 if(array[i]>array[j])
                 {
                     temp=array[i];
                     array[i] = array[j];
                     array[j] = temp;
                 }
             }
         }
         System.out.println("Lowest grade is " + array[0]);
         return array[0];
     }    
}

The constructor you shared takes a double[] . 你共享的构造函数需要一个double[] It seems like you forgot to pass the grades array you created: 好像你忘了传递你创建的grades数组:

double[] grades = {98.7, 77.9, 90, 83, 67, 33, 81, 90 };
HomeworkGrades cls = new HomeworkGrades(grades);

You forgot to set the parameter for your new HomeworkGrades() -instance. 您忘了为new HomeworkGrades() instance设置参数。

Therefore 因此

double[] grades = {98.7, 77.9, 90, 83, 67, 33, 81, 90 };
HomeworkGrades cls = new HomeworkGrades();

should be 应该

double[] grades = {98.7, 77.9, 90, 83, 67, 33, 81, 90 };
HomeworkGrades cls = new HomeworkGrades(grades);

Besides... You are doing some crazy stuff while initializing HomeworkGrades... 除此之外......在初始化HomeworkGrades时你正在做一些疯狂的事情......

This will work just fine: 这将工作得很好:

    double[] HWgrades;

    public HomeworkGrades(double [] gradesEarned) {
        HWgrades =gradesEarned;
    }

You aren't even using private double[] grades; 你甚至没有使用private double[] grades; and you don't have to set your array array.length times in order to get the array at the end. 并且您不必设置数组array.length时间以便最后获取数组。 Just do it once. 做一次吧。

EDIT In order to print your Average or whatever you want from the instance just get the double and print it in System.out.println() 编辑为了从实例中打印您的平​​均值或任何您想要的内容,只需获取双精度并在System.out.println()中打印它

    public static void main(String[] args) {
        double[] grades = {98.7, 77.9, 90, 83, 67, 33, 81, 90};
        HomeworkGrades cls = new HomeworkGrades(grades);
        System.out.println("Average: " + cls.calcAverage());
    }

EDIT2 Your function to get the lowest grade is strange... Just use this istead: EDIT2你获得最低成绩的功能很奇怪......只需使用此功能:

    public double calcLowestGrade() {
        double lowest = Double.MAX_VALUE;
        for (double d : HWgrades) {
            if (d < lowest) {
                lowest = d;
            }
        }
        return lowest;
    }

One mistake was that you made the function static and therefore it couldn't get the HWgrades array. 一个错误是你使函数静态,因此它无法获得HWgrades数组。 Furthermore you don't have to create a new array because you already gave the object the array in the beginning. 此外,您不必创建新数组,因为您已在开头为对象提供了数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM