问题召回方法。不确定我哪里出错了

时间:2018-03-22 21:08:29

标签: java methods

下面是我的代码,我的错误显示在旁边。我不确定在回忆我的方法时哪里出错了,或者甚至是问题。

import java.util.Scanner;

public class HurlerUse
{
    static Hurler[] hurlerArray;

    // find lowest score (static method)    
    public static int findLow(Hurler[] hurlerArray)
    {
         for(int i = 0; i < hurlerArray.length; i++)
         {
            int lowest = 0;
            int index = 0;
            for(int j=0; j<hurlerArray.length; j++)
            {
                int current = hurlerArray[i].totalPoints();// issue with         my method 'totalPoints' 
            if(current < lowest)
            {
                lowest = current;
                index = i;
            }
        }
            return index;
    }


}


//main code
public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    Hurler[] hurlerArray = new Hurler[5];

    for (int i = 0; i <4; i++)
    {
        hurlerArray[i] = new Hurler();
        System.out.println ("Enter Hurler Name:");
        hurlerArray[i].setName(sc.nextLine()); 
        hurlerArray[i].setGoalsScored(sc.nextInt()); 
        System.out.println("Enter the hurler's goals scored");
        hurlerArray[i].setPointsScored(sc.nextInt()); 
        System.out.println("Enter the hurler's points scored");
    }

    for(int i=0;i< hurlerArray.length; i++)
    {
        hurlerArray[i] = new Hurler(MyName, MyGoalsScored, MyPointsScored);// issue with all 3 objects in the brackets but im unsure of how to fix them
    }


    System.out.println("The lowest scoring hurler was " + hurlerArray[findLow(hurlerArray)].getName());// error with my code here I  think it is in the method
}

}//end of class

我知道nyName,myGoalsScored,myPointsScored是不正确的,但任何人都可以解释原因吗?

这是随附的课程页面

public class Hurler
{

private String name;
private int goalsScored;
private int pointsScored;


public Hurler() //constructor default
{
    name ="";
    goalsScored = 0;
    pointsScored = 0;
}

public Hurler(String myName, int myGoalsScored, int myPointsScored) // specific constructor 
{
    name = myName;
    goalsScored = myGoalsScored;
    pointsScored = myPointsScored;

}

//get and set name
public String getMyName()
{
    return name;
}

    public void setName(String myName)
{
    name = myName;
}

//get and set goals scored
public int getGoalsScored()
{
    return goalsScored;
}

public void setGoalsScored(int myGoalsScored)
{
    goalsScored = myGoalsScored;
}

// get and set points scored
public int getPointsScored()
{
    return pointsScored;
}

public void setPointsScored(int myPointsScored)
{
    pointsScored = myPointsScored;
}

public int totalPoints(int myGoalsScored, int myPointsScored)
{
    int oneGoal = 3;
    int onePoint = 1; 
    int totalPoints = ((goalsScored * oneGoal) + (pointsScored * onePoint));
    {
        return totalPoints;
    }
}

} //班级的结尾

1 个答案:

答案 0 :(得分:1)

  1. 在没有参数的情况下调用totalPoints(),而Hurler类中的方法totalPoints(int,int)需要两个int参数。
  2. 对象MyName,MyGoalsScored,MyPointsScored根本没有声明。
  3. 你调用getName()方法,而在Hurler类中你没有。有方法getMyName(),也许你想调用那个。